diff options
| author | Simon Robertshaw <simon@hardwired.org.uk> | 2012-09-01 16:55:27 (GMT) |
|---|---|---|
| committer | Simon Robertshaw <simon@hardwired.org.uk> | 2012-09-01 16:55:27 (GMT) |
| commit | 618e29d5d487a2215628b5cf70ccc72462878201 (patch) | |
| tree | b7a940375cf681162e9e6522cfae3e427c9465e5 /src/game | |
| parent | 0a67e560f4ca85f16c24fa90cbcefa7e07bac62f (diff) | |
| download | powder-618e29d5d487a2215628b5cf70ccc72462878201.zip powder-618e29d5d487a2215628b5cf70ccc72462878201.tar.gz | |
Colour picker and presets for decorations
Diffstat (limited to 'src/game')
| -rw-r--r-- | src/game/GameController.cpp | 22 | ||||
| -rw-r--r-- | src/game/GameController.h | 2 | ||||
| -rw-r--r-- | src/game/GameModel.cpp | 54 | ||||
| -rw-r--r-- | src/game/GameModel.h | 16 | ||||
| -rw-r--r-- | src/game/GameView.cpp | 239 | ||||
| -rw-r--r-- | src/game/GameView.h | 15 |
6 files changed, 196 insertions, 152 deletions
diff --git a/src/game/GameController.cpp b/src/game/GameController.cpp index ffa763f..910673e 100644 --- a/src/game/GameController.cpp +++ b/src/game/GameController.cpp @@ -17,6 +17,7 @@ #include "GameModelException.h" #include "simulation/Air.h" #include "elementsearch/ElementSearchActivity.h" +#include "colourpicker/ColourPickerActivity.h" #include "update/UpdateActivity.h" #include "Notification.h" #include "filebrowser/FileBrowserActivity.h" @@ -845,6 +846,11 @@ void GameController::SetDecoration() gameModel->SetDecoration(!gameModel->GetDecoration()); } +void GameController::SetActiveColourPreset(int preset) +{ + gameModel->SetActiveColourPreset(preset); +} + void GameController::SetColour(ui::Colour colour) { gameModel->SetColourSelectorColour(colour); @@ -965,6 +971,22 @@ void GameController::OpenElementSearch() new ElementSearchActivity(gameModel, toolList); } +void GameController::OpenColourPicker() +{ + class ColourPickerCallback: public ColourPickedCallback + { + GameController * c; + public: + ColourPickerCallback(GameController * _c): c(_c) {} + virtual ~ColourPickerCallback() {}; + virtual void ColourPicked(ui::Colour colour) + { + c->SetColour(colour); + } + }; + new ColourPickerActivity(gameModel->GetColourSelectorColour(), new ColourPickerCallback(this)); +} + void GameController::OpenTags() { if(gameModel->GetUser().ID) diff --git a/src/game/GameController.h b/src/game/GameController.h index af3f1bf..46941e7 100644 --- a/src/game/GameController.h +++ b/src/game/GameController.h @@ -96,6 +96,7 @@ public: void SetDecoration(); void SetActiveMenu(Menu * menu); void SetActiveTool(int toolSelection, Tool * tool); + void SetActiveColourPreset(int preset); void SetColour(ui::Colour colour); void SetToolStrength(float value); void LoadSaveFile(SaveFile * file); @@ -112,6 +113,7 @@ public: void SaveAsCurrent(); void OpenStamps(); void OpenElementSearch(); + void OpenColourPicker(); void PlaceSave(ui::Point position); void ClearSim(); void ReloadSim(); diff --git a/src/game/GameModel.cpp b/src/game/GameModel.cpp index 1b7e2aa..2870d0d 100644 --- a/src/game/GameModel.cpp +++ b/src/game/GameModel.cpp @@ -26,7 +26,8 @@ GameModel::GameModel(): stamp(NULL), placeSave(NULL), colour(255, 0, 0, 255), - toolStrength(1.0f) + toolStrength(1.0f), + activeColourPreset(-1) { sim = new Simulation(); ren = new Renderer(ui::Engine::Ref().g, sim); @@ -88,6 +89,14 @@ GameModel::GameModel(): unsigned char colourA = min(Client::Ref().GetPrefInteger("Decoration.Alpha", 255), 255); SetColourSelectorColour(ui::Colour(colourR, colourG, colourB, colourA)); + + colourPresets.push_back(ui::Colour(255, 255, 255)); + colourPresets.push_back(ui::Colour(0, 255, 255)); + colourPresets.push_back(ui::Colour(255, 0, 255)); + colourPresets.push_back(ui::Colour(255, 255, 0)); + colourPresets.push_back(ui::Colour(255, 0, 0)); + colourPresets.push_back(ui::Colour(0, 255, 0)); + colourPresets.push_back(ui::Colour(0, 0, 255)); } GameModel::~GameModel() @@ -338,6 +347,8 @@ void GameModel::AddObserver(GameView * observer){ observer->NotifyZoomChanged(this); observer->NotifyColourSelectorVisibilityChanged(this); observer->NotifyColourSelectorColourChanged(this); + observer->NotifyColourPresetsChanged(this); + observer->NotifyColourActivePresetChanged(this); observer->NotifyQuickOptionsChanged(this); observer->NotifyLastToolChanged(this); UpdateQuickOptions(); @@ -560,6 +571,31 @@ int GameModel::GetZoomFactor() return ren->ZFACTOR; } +void GameModel::SetActiveColourPreset(int preset) +{ + activeColourPreset = preset; + notifyColourActivePresetChanged(); +} + +int GameModel::GetActiveColourPreset() +{ + return activeColourPreset; +} + +void GameModel::SetPresetColour(ui::Colour colour) +{ + if(activeColourPreset >= 0 && activeColourPreset < colourPresets.size()) + { + colourPresets[activeColourPreset] = colour; + notifyColourPresetsChanged(); + } +} + +std::vector<ui::Colour> GameModel::GetColourPresets() +{ + return colourPresets; +} + void GameModel::SetColourSelectorVisibility(bool visibility) { if(colourSelector != visibility) @@ -757,6 +793,22 @@ void GameModel::notifyNotificationsChanged() } } +void GameModel::notifyColourPresetsChanged() +{ + for(std::vector<GameView*>::iterator iter = observers.begin(); iter != observers.end(); ++iter) + { + (*iter)->NotifyColourPresetsChanged(this); + } +} + +void GameModel::notifyColourActivePresetChanged() +{ + for(std::vector<GameView*>::iterator iter = observers.begin(); iter != observers.end(); ++iter) + { + (*iter)->NotifyColourActivePresetChanged(this); + } +} + void GameModel::notifyColourSelectorColourChanged() { for(int i = 0; i < observers.size(); i++) diff --git a/src/game/GameModel.h b/src/game/GameModel.h index 847e940..a46bfd3 100644 --- a/src/game/GameModel.h +++ b/src/game/GameModel.h @@ -60,11 +60,14 @@ private: Tool * lastTool; Tool * activeTools[3]; User currentUser; - bool colourSelector; - ui::Colour colour; float toolStrength; std::deque<Snapshot*> history; + int activeColourPreset; + std::vector<ui::Colour> colourPresets; + bool colourSelector; + ui::Colour colour; + std::string infoTip; std::string toolTip; //bool zoomEnabled; @@ -83,6 +86,8 @@ private: void notifyPlaceSaveChanged(); void notifyColourSelectorColourChanged(); void notifyColourSelectorVisibilityChanged(); + void notifyColourPresetsChanged(); + void notifyColourActivePresetChanged(); void notifyNotificationsChanged(); void notifyLogChanged(string entry); void notifyInfoTipChanged(); @@ -93,6 +98,13 @@ public: GameModel(); ~GameModel(); + void SetActiveColourPreset(int preset); + int GetActiveColourPreset(); + + void SetPresetColour(ui::Colour colour); + + std::vector<ui::Colour> GetColourPresets(); + void SetColourSelectorVisibility(bool visibility); bool GetColourSelectorVisibility(); diff --git a/src/game/GameView.cpp b/src/game/GameView.cpp index 7e15e07..943e4af 100644 --- a/src/game/GameView.cpp +++ b/src/game/GameView.cpp @@ -390,49 +390,6 @@ GameView::GameView(): pauseButton->SetActionCallback(new PauseAction(this)); AddComponent(pauseButton); - class ColourChange : public ui::SliderAction, public ui::TextboxAction - { - GameView * v; - public: - ColourChange(GameView * _v) { v = _v; } - void ValueChangedCallback(ui::Slider * sender) - { - v->changeColourSlider(); - } - - void TextChangedCallback(ui::Textbox * sender) - { - v->changeColourText(); - } - }; - colourRSlider = new ui::Slider(ui::Point(5, Size.Y-39), ui::Point(50, 14), 255); - colourRSlider->SetActionCallback(new ColourChange(this)); - colourRValue = new ui::Textbox(ui::Point(60, Size.Y-41), ui::Point(25, 17), "255"); - colourRValue->SetActionCallback(new ColourChange(this)); - colourRValue->SetLimit(3); - colourRValue->SetInputType(ui::Textbox::Number); - - colourGSlider = new ui::Slider(ui::Point(95, Size.Y-39), ui::Point(50, 14), 255); - colourGSlider->SetActionCallback(new ColourChange(this)); - colourGValue = new ui::Textbox(ui::Point(150, Size.Y-41), ui::Point(25, 17), "255"); - colourGValue->SetActionCallback(new ColourChange(this)); - colourGValue->SetLimit(3); - colourGValue->SetInputType(ui::Textbox::Number); - - colourBSlider = new ui::Slider(ui::Point(185, Size.Y-39), ui::Point(50, 14), 255); - colourBSlider->SetActionCallback(new ColourChange(this)); - colourBValue = new ui::Textbox(ui::Point(240, Size.Y-41), ui::Point(25, 17), "255"); - colourBValue->SetActionCallback(new ColourChange(this)); - colourBValue->SetLimit(3); - colourBValue->SetInputType(ui::Textbox::Number); - - colourASlider = new ui::Slider(ui::Point(275, Size.Y-39), ui::Point(50, 14), 255); - colourASlider->SetActionCallback(new ColourChange(this)); - colourAValue = new ui::Textbox(ui::Point(330, Size.Y-41), ui::Point(25, 17), "255"); - colourAValue->SetActionCallback(new ColourChange(this)); - colourAValue->SetLimit(3); - colourAValue->SetInputType(ui::Textbox::Number); - class ElementSearchAction : public ui::ButtonAction { GameView * v; @@ -448,6 +405,19 @@ GameView::GameView(): tempButton->SetActionCallback(new ElementSearchAction(this)); AddComponent(tempButton); + class ColourPickerAction : public ui::ButtonAction + { + GameView * v; + public: + ColourPickerAction(GameView * _v) { v = _v; } + void ActionCallback(ui::Button * sender) + { + v->c->OpenColourPicker(); + } + }; + colourPicker = new ui::Button(ui::Point((XRES/2)-8, YRES+1), ui::Point(16, 16), "", "Pick Colour"); + colourPicker->SetActionCallback(new ColourPickerAction(this)); + //Render mode presets. Possibly load from config in future? renderModePresets = new RenderPreset[10]; @@ -506,29 +476,18 @@ GameView::~GameView() { delete[] renderModePresets; - if(!colourRSlider->GetParentWindow()) - delete colourRSlider; - - if(!colourGSlider->GetParentWindow()) - delete colourGSlider; + if(!colourPicker->GetParentWindow()) + delete colourPicker; - if(!colourBSlider->GetParentWindow()) - delete colourBSlider; - - if(!colourASlider->GetParentWindow()) - delete colourASlider; - - if(!colourRValue->GetParentWindow()) - delete colourRValue; - - if(!colourGValue->GetParentWindow()) - delete colourGValue; - - if(!colourBValue->GetParentWindow()) - delete colourBValue; + for(std::vector<ToolButton*>::iterator iter = colourPresets.begin(), end = colourPresets.end(); iter != end; ++iter) + { + ToolButton * button = *iter; + if(!button->GetParentWindow()) + { + delete button; + } - if(!colourAValue->GetParentWindow()) - delete colourAValue; + } if(placeSaveThumb) delete placeSaveThumb; @@ -761,71 +720,96 @@ void GameView::NotifyToolListChanged(GameModel * sender) void GameView::NotifyColourSelectorVisibilityChanged(GameModel * sender) { - RemoveComponent(colourRSlider); - colourRSlider->SetParentWindow(NULL); - RemoveComponent(colourRValue); - colourRValue->SetParentWindow(NULL); - - RemoveComponent(colourGSlider); - colourGSlider->SetParentWindow(NULL); - RemoveComponent(colourGValue); - colourGValue->SetParentWindow(NULL); - - RemoveComponent(colourBSlider); - colourBSlider->SetParentWindow(NULL); - RemoveComponent(colourBValue); - colourBValue->SetParentWindow(NULL); - - RemoveComponent(colourASlider); - colourASlider->SetParentWindow(NULL); - RemoveComponent(colourAValue); - colourAValue->SetParentWindow(NULL); + for(std::vector<ToolButton*>::iterator iter = colourPresets.begin(), end = colourPresets.end(); iter != end; ++iter) + { + ToolButton * button = *iter; + RemoveComponent(button); + button->SetParentWindow(NULL); + } + + RemoveComponent(colourPicker); + colourPicker->SetParentWindow(NULL); if(sender->GetColourSelectorVisibility()) { - AddComponent(colourRSlider); - AddComponent(colourRValue); + for(std::vector<ToolButton*>::iterator iter = colourPresets.begin(), end = colourPresets.end(); iter != end; ++iter) + { + ToolButton * button = *iter; + AddComponent(button); + } + AddComponent(colourPicker); + } +} - AddComponent(colourGSlider); - AddComponent(colourGValue); +void GameView::NotifyColourPresetsChanged(GameModel * sender) +{ + class ColourPresetAction: public ui::ButtonAction + { + GameView * v; + public: + int preset; + ColourPresetAction(GameView * _v, int preset) : preset(preset) { v = _v; } + void ActionCallback(ui::Button * sender_) + { + ToolButton *sender = (ToolButton*)sender_; + if(sender->GetSelectionState() == 0) + { + v->c->SetActiveColourPreset(preset); + v->c->SetColour(sender->Appearance.BackgroundInactive); + } + else + sender->SetSelectionState(0); + } + }; - AddComponent(colourBSlider); - AddComponent(colourBValue); - AddComponent(colourASlider); - AddComponent(colourAValue); + for(std::vector<ToolButton*>::iterator iter = colourPresets.begin(), end = colourPresets.end(); iter != end; ++iter) + { + ToolButton * button = *iter; + RemoveComponent(button); + delete button; } + colourPresets.clear(); + + int currentX = 5; + std::vector<ui::Colour> colours = sender->GetColourPresets(); + int i = 0; + for(std::vector<ui::Colour>::iterator iter = colours.begin(), end = colours.end(); iter != end; ++iter) + { + ToolButton * tempButton = new ToolButton(ui::Point(currentX, YRES+1), ui::Point(30, 18), ""); + tempButton->Appearance.BackgroundInactive = *iter; + tempButton->SetActionCallback(new ColourPresetAction(this, i)); + + currentX += 31; + + if(sender->GetColourSelectorVisibility()) + AddComponent(tempButton); + colourPresets.push_back(tempButton); + + i++; + } + NotifyColourActivePresetChanged(sender); +} +void GameView::NotifyColourActivePresetChanged(GameModel * sender) +{ + for(int i = 0; i < colourPresets.size(); i++) + { + if(sender->GetActiveColourPreset() == i) + { + colourPresets[i]->SetSelectionState(0); //Primary + } + else + { + colourPresets[i]->SetSelectionState(-1); + } + } } void GameView::NotifyColourSelectorColourChanged(GameModel * sender) { - std::string intR, intG, intB, intA; - - intR = format::NumberToString<int>(sender->GetColourSelectorColour().Red); - intG = format::NumberToString<int>(sender->GetColourSelectorColour().Green); - intB = format::NumberToString<int>(sender->GetColourSelectorColour().Blue); - intA = format::NumberToString<int>(sender->GetColourSelectorColour().Alpha); - - colourRSlider->SetValue(sender->GetColourSelectorColour().Red); - colourRSlider->SetColour(ui::Colour(0, 0, 0), ui::Colour(255, 0, 0)); - if(!colourRValue->IsFocused()) - colourRValue->SetText(intR); - - colourGSlider->SetValue(sender->GetColourSelectorColour().Green); - colourGSlider->SetColour(ui::Colour(0, 0, 0), ui::Colour(0, 255, 0)); - if(!colourGValue->IsFocused()) - colourGValue->SetText(intG); - - colourBSlider->SetValue(sender->GetColourSelectorColour().Blue); - colourBSlider->SetColour(ui::Colour(0, 0, 0), ui::Colour(0, 0, 255)); - if(!colourBValue->IsFocused()) - colourBValue->SetText(intB); - - colourASlider->SetValue(sender->GetColourSelectorColour().Alpha); - colourASlider->SetColour(ui::Colour(0, 0, 0), ui::Colour(sender->GetColourSelectorColour().Red, sender->GetColourSelectorColour().Green, sender->GetColourSelectorColour().Blue)); - if(!colourAValue->IsFocused()) - colourAValue->SetText(intA); + colourPicker->Appearance.BackgroundInactive = sender->GetColourSelectorColour(); + colourPicker->Appearance.BackgroundHover = sender->GetColourSelectorColour(); } void GameView::NotifyRendererChanged(GameModel * sender) @@ -1210,9 +1194,6 @@ void GameView::BeginStampSelection() void GameView::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt) { - if(colourRValue->IsFocused() || colourGValue->IsFocused() || colourBValue->IsFocused() || colourAValue->IsFocused()) - return; - if(introText > 50) { introText = 50; @@ -1432,9 +1413,6 @@ void GameView::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool void GameView::OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt) { - if(colourRValue->IsFocused() || colourGValue->IsFocused() || colourBValue->IsFocused() || colourAValue->IsFocused()) - return; - if(!isMouseDown) drawMode = DrawPoints; else @@ -1691,21 +1669,6 @@ void GameView::NotifyPlaceSaveChanged(GameModel * sender) } } -void GameView::changeColourSlider() -{ - c->SetColour(ui::Colour(colourRSlider->GetValue(), colourGSlider->GetValue(), colourBSlider->GetValue(), colourASlider->GetValue())); -} - -void GameView::changeColourText() -{ - c->SetColour(ui::Colour( - std::min(255U, format::StringToNumber<unsigned int>(colourRValue->GetText())), - std::min(255U, format::StringToNumber<unsigned int>(colourGValue->GetText())), - std::min(255U, format::StringToNumber<unsigned int>(colourBValue->GetText())), - std::min(255U, format::StringToNumber<unsigned int>(colourAValue->GetText()))) - ); -} - void GameView::enableShiftBehaviour() { if(!shiftBehaviour) diff --git a/src/game/GameView.h b/src/game/GameView.h index 32ccb86..7e5125b 100644 --- a/src/game/GameView.h +++ b/src/game/GameView.h @@ -88,15 +88,8 @@ private: ui::Button * pauseButton; ui::Point currentMouse; - ui::Slider * colourRSlider; - ui::Slider * colourGSlider; - ui::Slider * colourBSlider; - ui::Slider * colourASlider; - - ui::Textbox * colourRValue; - ui::Textbox * colourGValue; - ui::Textbox * colourBValue; - ui::Textbox * colourAValue; + ui::Button * colourPicker; + vector<ToolButton*> colourPresets; bool drawModeReset; ui::Point drawPoint1; @@ -116,8 +109,6 @@ private: int lastOffset; void setToolButtonOffset(int offset); - void changeColourSlider(); - void changeColourText(); virtual ui::Point lineSnapCoords(ui::Point point1, ui::Point point2); virtual ui::Point rectSnapCoords(ui::Point point1, ui::Point point2); @@ -157,6 +148,8 @@ public: void NotifyZoomChanged(GameModel * sender); void NotifyColourSelectorVisibilityChanged(GameModel * sender); void NotifyColourSelectorColourChanged(GameModel * sender); + void NotifyColourPresetsChanged(GameModel * sender); + void NotifyColourActivePresetChanged(GameModel * sender); void NotifyPlaceSaveChanged(GameModel * sender); void NotifyNotificationsChanged(GameModel * sender); void NotifyLogChanged(GameModel * sender, string entry); |
