void EyedropperCommand::onExecute(Context* context) { Widget* widget = gui::Manager::getDefault()->getMouse(); if (!widget || widget->type != editor_type()) return; Editor* editor = static_cast<Editor*>(widget); Sprite* sprite = editor->getSprite(); if (!sprite) return; // pixel position to get int x, y; editor->screenToEditor(jmouse_x(0), jmouse_y(0), &x, &y); // get the color from the image Color color = Color::fromImage(sprite->getPixelFormat(), sprite->getPixel(x, y)); // TODO replace the color in the "context", not directly from the color-bar // set the color of the color-bar if (m_background) app_get_colorbar()->setBgColor(color); else app_get_colorbar()->setFgColor(color); }
void ChangeColorCommand::onExecute(Context* context) { ColorBar* colorbar = app_get_colorbar(); Color color = m_background ? colorbar->getBgColor(): colorbar->getFgColor(); switch (m_change) { case None: // do nothing break; case IncrementIndex: if (color.getType() == Color::IndexType) { int index = color.getIndex(); if (index < 255) // TODO use sprite palette limit color = Color::fromIndex(index+1); } else color = Color::fromIndex(0); break; case DecrementIndex: if (color.getType() == Color::IndexType) { int index = color.getIndex(); if (index > 0) color = Color::fromIndex(index-1); } else color = Color::fromIndex(0); break; } if (m_background) colorbar->setBgColor(color); else colorbar->setFgColor(color); }
void FlattenLayersCommand::onExecute(Context* context) { ActiveDocumentWriter document(context); Sprite* sprite = document->getSprite(); int bgcolor = color_utils::color_for_image(app_get_colorbar()->getBgColor(), sprite->getImgType()); { UndoTransaction undoTransaction(document, "Flatten Layers"); undoTransaction.flattenLayers(bgcolor); undoTransaction.commit(); } update_screen_for_document(document); }
/** * Shows the "New Sprite" dialog. */ void NewFileCommand::onExecute(Context* context) { JWidget width, height, radio1, radio2, radio3, colors, ok, bg_box; int imgtype, w, h, bg, ncolors; char buf[1024]; Color bg_table[] = { Color::fromMask(), Color::fromRgb(0, 0, 0), Color::fromRgb(255, 255, 255), Color::fromRgb(255, 0, 255), app_get_colorbar()->getBgColor() }; // Load the window widget FramePtr window(load_widget("new_sprite.xml", "new_sprite")); get_widgets(window, "width", &width, "height", &height, "radio1", &radio1, "radio2", &radio2, "radio3", &radio3, "colors", &colors, "ok_button", &ok, "bg_box", &bg_box, NULL); // Default values: Indexed, 320x240, Background color imgtype = get_config_int("NewSprite", "Type", IMAGE_INDEXED); imgtype = MID(IMAGE_RGB, imgtype, IMAGE_INDEXED); w = get_config_int("NewSprite", "Width", 320); h = get_config_int("NewSprite", "Height", 240); bg = get_config_int("NewSprite", "Background", 4); // Default = Background color ncolors = get_config_int("NewSprite", "Colors", 256); width->setTextf("%d", MAX(1, w)); height->setTextf("%d", MAX(1, h)); colors->setTextf("%d", MID(2, ncolors, 256)); // Select image-type switch (imgtype) { case IMAGE_RGB: radio1->setSelected(true); break; case IMAGE_GRAYSCALE: radio2->setSelected(true); break; case IMAGE_INDEXED: radio3->setSelected(true); break; } // Select background color jlistbox_select_index(bg_box, bg); // Open the window window->open_window_fg(); if (window->get_killer() == ok) { bool ok = false; // Get the options if (radio1->isSelected()) imgtype = IMAGE_RGB; else if (radio2->isSelected()) imgtype = IMAGE_GRAYSCALE; else if (radio3->isSelected()) imgtype = IMAGE_INDEXED; w = width->getTextInt(); h = height->getTextInt(); ncolors = colors->getTextInt(); bg = jlistbox_get_selected_index(bg_box); w = MID(1, w, 9999); h = MID(1, h, 9999); ncolors = MID(2, ncolors, 256); // Select the color Color color = Color::fromMask(); if (bg >= 0 && bg <= 4) { color = bg_table[bg]; ok = true; } if (ok) { // Save the configuration set_config_int("NewSprite", "Type", imgtype); set_config_int("NewSprite", "Width", w); set_config_int("NewSprite", "Height", h); set_config_int("NewSprite", "Background", bg); // Create the new sprite ASSERT(imgtype == IMAGE_RGB || imgtype == IMAGE_GRAYSCALE || imgtype == IMAGE_INDEXED); ASSERT(w >= 1 && w <= 9999); ASSERT(h >= 1 && h <= 9999); UniquePtr<Document> document( Document::createBasicDocument(imgtype, w, h, (imgtype == IMAGE_INDEXED ? ncolors: 256))); Sprite* sprite(document->getSprite()); get_default_palette()->copyColorsTo(sprite->getCurrentPalette()); usprintf(buf, "Sprite-%04d", ++_sprite_counter); document->setFilename(buf); // If the background color isn't transparent, we have to // convert the `Layer 1' in a `Background' if (color.getType() != Color::MaskType) { Sprite* sprite = document->getSprite(); ASSERT(sprite->getCurrentLayer() && sprite->getCurrentLayer()->is_image()); static_cast<LayerImage*>(sprite->getCurrentLayer())->configureAsBackground(); image_clear(sprite->getCurrentImage(), color_utils::color_for_image(color, imgtype)); } // Show the sprite to the user context->addDocument(document); // Release the document as it is already owned by the context. // And put the document in a reliable editor. set_document_in_more_reliable_editor(document.release()); } } }
bool StandbyState::onMouseWheel(Editor* editor, Message* msg) { int dz = jmouse_z(1) - jmouse_z(0); WHEEL_ACTION wheelAction = WHEEL_NONE; bool scrollBigSteps = false; // Without modifiers if (!(msg->any.shifts & (KB_SHIFT_FLAG | KB_ALT_FLAG | KB_CTRL_FLAG))) { wheelAction = WHEEL_ZOOM; } else { #if 1 // TODO make it configurable if (has_shifts(msg, KB_ALT_FLAG)) { if (has_shifts(msg, KB_SHIFT_FLAG)) wheelAction = WHEEL_BG; else wheelAction = WHEEL_FG; } else if (has_shifts(msg, KB_CTRL_FLAG)) { wheelAction = WHEEL_FRAME; } #else if (has_shifts(msg, KB_CTRL_FLAG)) wheelAction = WHEEL_HSCROLL; else wheelAction = WHEEL_VSCROLL; if (has_shifts(msg, KB_SHIFT_FLAG)) scrollBigSteps = true; #endif } switch (wheelAction) { case WHEEL_NONE: // Do nothing break; case WHEEL_FG: // if (m_state == EDITOR_STATE_STANDBY) { int newIndex = 0; if (app_get_colorbar()->getFgColor().getType() == Color::IndexType) { newIndex = app_get_colorbar()->getFgColor().getIndex() + dz; newIndex = MID(0, newIndex, 255); } app_get_colorbar()->setFgColor(Color::fromIndex(newIndex)); } break; case WHEEL_BG: // if (m_state == EDITOR_STATE_STANDBY) { int newIndex = 0; if (app_get_colorbar()->getBgColor().getType() == Color::IndexType) { newIndex = app_get_colorbar()->getBgColor().getIndex() + dz; newIndex = MID(0, newIndex, 255); } app_get_colorbar()->setBgColor(Color::fromIndex(newIndex)); } break; case WHEEL_FRAME: // if (m_state == EDITOR_STATE_STANDBY) { Command* command = CommandsModule::instance()->getCommandByName ((dz < 0) ? CommandId::GotoNextFrame: CommandId::GotoPreviousFrame); if (command) UIContext::instance()->executeCommand(command, NULL); } break; case WHEEL_ZOOM: { int zoom = MID(MIN_ZOOM, editor->getZoom()-dz, MAX_ZOOM); if (editor->getZoom() != zoom) editor->setZoomAndCenterInMouse(zoom, msg->mouse.x, msg->mouse.y); break; } case WHEEL_HSCROLL: case WHEEL_VSCROLL: { View* view = View::getView(editor); gfx::Rect vp = view->getViewportBounds(); int dx = 0; int dy = 0; if (wheelAction == WHEEL_HSCROLL) { dx = dz * vp.w; } else { dy = dz * vp.h; } if (scrollBigSteps) { dx /= 2; dy /= 2; } else { dx /= 10; dy /= 10; } gfx::Point scroll = view->getViewScroll(); editor->hideDrawingCursor(); editor->setEditorScroll(scroll.x+dx, scroll.y+dy, true); editor->showDrawingCursor(); break; } } return true; }