forked from Kolyah35/minecraft-pe-0.6.1
FIX: Rewrite options
This commit is contained in:
@@ -9,13 +9,20 @@ class GuiElement : public GuiComponent {
|
||||
public:
|
||||
GuiElement(bool active=false, bool visible=true, int x = 0, int y = 0, int width=24, int height=24);
|
||||
virtual ~GuiElement() {}
|
||||
virtual void tick(Minecraft* minecraft) {}
|
||||
|
||||
virtual void tick(Minecraft* minecraft) {}
|
||||
virtual void render(Minecraft* minecraft, int xm, int ym) { }
|
||||
virtual void setupPositions() {}
|
||||
|
||||
virtual void mouseClicked(Minecraft* minecraft, int x, int y, int buttonNum) {}
|
||||
virtual void mouseReleased(Minecraft* minecraft, int x, int y, int buttonNum) {}
|
||||
virtual void keyPressed(Minecraft* minecraft, int key) {}
|
||||
virtual void charPressed(Minecraft* minecraft, char key) {}
|
||||
|
||||
virtual bool pointInside(int x, int y);
|
||||
|
||||
void setVisible(bool visible);
|
||||
|
||||
bool active;
|
||||
bool visible;
|
||||
int x;
|
||||
|
||||
@@ -52,3 +52,15 @@ void GuiElementContainer::mouseReleased( Minecraft* minecraft, int x, int y, int
|
||||
(*it)->mouseReleased(minecraft, x, y, buttonNum);
|
||||
}
|
||||
}
|
||||
|
||||
void GuiElementContainer::keyPressed(Minecraft* minecraft, int key) {
|
||||
for(std::vector<GuiElement*>::iterator it = children.begin(); it != children.end(); ++it) {
|
||||
(*it)->keyPressed(minecraft, key);
|
||||
}
|
||||
}
|
||||
|
||||
void GuiElementContainer::charPressed(Minecraft* minecraft, char key) {
|
||||
for(std::vector<GuiElement*>::iterator it = children.begin(); it != children.end(); ++it) {
|
||||
(*it)->charPressed(minecraft, key);
|
||||
}
|
||||
}
|
||||
@@ -17,8 +17,9 @@ public:
|
||||
virtual void tick( Minecraft* minecraft );
|
||||
|
||||
virtual void mouseClicked( Minecraft* minecraft, int x, int y, int buttonNum );
|
||||
|
||||
virtual void mouseReleased( Minecraft* minecraft, int x, int y, int buttonNum );
|
||||
virtual void keyPressed(Minecraft* minecraft, int key);
|
||||
virtual void charPressed(Minecraft* minecraft, char key);
|
||||
|
||||
protected:
|
||||
std::vector<GuiElement*> children;
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "../../../platform/log.h"
|
||||
#include "../../../util/Mth.h"
|
||||
#include "../../renderer/Textures.h"
|
||||
#include <client/Option.h>
|
||||
|
||||
|
||||
ImageButton::ImageButton(int id, const std::string& msg)
|
||||
@@ -112,43 +113,17 @@ void ImageButton::render(Minecraft* minecraft, int xm, int ym) {
|
||||
//
|
||||
// A toggleable Button
|
||||
//
|
||||
OptionButton::OptionButton(const Options::Option* option)
|
||||
: _option(option),
|
||||
_isFloat(false),
|
||||
super(ButtonId, "")
|
||||
{
|
||||
}
|
||||
|
||||
OptionButton::OptionButton(const Options::Option* option, float onValue, float offValue)
|
||||
: _option(option),
|
||||
_isFloat(true),
|
||||
_onValue(onValue),
|
||||
_offValue(offValue),
|
||||
super(ButtonId, "")
|
||||
{
|
||||
}
|
||||
|
||||
bool OptionButton::isSecondImage(bool hovered) {
|
||||
return _secondImage;
|
||||
}
|
||||
OptionButton::OptionButton(OptionId option) : m_optId(option), super(ButtonId, "") {}
|
||||
|
||||
void OptionButton::toggle(Options* options) {
|
||||
if (_isFloat) {
|
||||
options->set(_option, (Mth::abs(_current - _onValue) < 0.01f) ? _offValue : _onValue);
|
||||
} else {
|
||||
options->toggle(_option, 1);
|
||||
}
|
||||
options->toggle(m_optId);
|
||||
|
||||
// Update graphics here
|
||||
updateImage(options);
|
||||
}
|
||||
|
||||
void OptionButton::updateImage(Options* options) {
|
||||
if (_isFloat) {
|
||||
_current = options->getProgressValue(_option);
|
||||
_secondImage = Mth::abs(_current - _onValue) < 0.01f;
|
||||
} else {
|
||||
_secondImage = options->getBooleanValue(_option);
|
||||
}
|
||||
_secondImage = options->getBooleanValue(m_optId);
|
||||
}
|
||||
|
||||
void OptionButton::mouseClicked( Minecraft* minecraft, int x, int y, int buttonNum ) {
|
||||
|
||||
@@ -77,28 +77,20 @@ class OptionButton: public ImageButton
|
||||
{
|
||||
typedef ImageButton super;
|
||||
public:
|
||||
OptionButton(const Options::Option* option);
|
||||
OptionButton(const Options::Option* option, float onValue, float offValue);
|
||||
OptionButton(OptionId optId);
|
||||
|
||||
void toggle(Options* options);
|
||||
void updateImage(Options* options);
|
||||
|
||||
static const int ButtonId = 9999999;
|
||||
protected:
|
||||
bool isSecondImage(bool hovered);
|
||||
bool isSecondImage(bool hovered) { return _secondImage; }
|
||||
|
||||
virtual void mouseClicked( Minecraft* minecraft, int x, int y, int buttonNum );
|
||||
|
||||
private:
|
||||
|
||||
const Options::Option* _option;
|
||||
OptionId m_optId;
|
||||
bool _secondImage;
|
||||
|
||||
// If not float, it's considered to be a boolean value
|
||||
bool _isFloat;
|
||||
float _onValue;
|
||||
float _offValue;
|
||||
float _current;
|
||||
};
|
||||
|
||||
|
||||
|
||||
22
src/client/gui/components/KeyOption.cpp
Normal file
22
src/client/gui/components/KeyOption.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#include "KeyOption.h"
|
||||
#include <client/Minecraft.h>
|
||||
|
||||
KeyOption::KeyOption(Minecraft* minecraft, OptionId optId)
|
||||
: Touch::TButton((int)optId, Keyboard::getKeyName(minecraft->options.getIntValue(optId))) {}
|
||||
|
||||
|
||||
void KeyOption::mouseClicked(Minecraft* minecraft, int x, int y, int buttonNum) {
|
||||
selected = isInside(x, y);
|
||||
msg = (selected)? "..." : Keyboard::getKeyName(minecraft->options.getIntValue((OptionId)id));
|
||||
}
|
||||
|
||||
void KeyOption::keyPressed(Minecraft* minecraft, int key) {
|
||||
if (!selected) return;
|
||||
|
||||
if (key != Keyboard::KEY_ESCAPE) {
|
||||
minecraft->options.set((OptionId)id, key);
|
||||
}
|
||||
|
||||
selected = false;
|
||||
msg = Keyboard::getKeyName(minecraft->options.getIntValue((OptionId)id));
|
||||
}
|
||||
14
src/client/gui/components/KeyOption.h
Normal file
14
src/client/gui/components/KeyOption.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
#include "Button.h"
|
||||
#include <client/Options.h>
|
||||
|
||||
class KeyOption : public Touch::TButton {
|
||||
public:
|
||||
KeyOption(Minecraft* minecraft, OptionId optId);
|
||||
|
||||
virtual void mouseClicked(Minecraft* minecraft, int x, int y, int buttonNum);
|
||||
virtual void released(int mx, int my) {}
|
||||
virtual void keyPressed(Minecraft* minecraft, int key);
|
||||
protected:
|
||||
bool m_captureMode;
|
||||
};
|
||||
@@ -4,6 +4,9 @@
|
||||
#include "OptionsItem.h"
|
||||
#include "Slider.h"
|
||||
#include "../../../locale/I18n.h"
|
||||
#include "TextOption.h"
|
||||
#include "KeyOption.h"
|
||||
|
||||
OptionsGroup::OptionsGroup( std::string labelID ) {
|
||||
label = I18n::get(labelID);
|
||||
}
|
||||
@@ -25,74 +28,88 @@ void OptionsGroup::setupPositions() {
|
||||
void OptionsGroup::render( Minecraft* minecraft, int xm, int ym ) {
|
||||
float padX = 10.0f;
|
||||
float padY = 5.0f;
|
||||
|
||||
minecraft->font->draw(label, (float)x + padX, (float)y + padY, 0xffffffff, false);
|
||||
|
||||
super::render(minecraft, xm, ym);
|
||||
}
|
||||
|
||||
OptionsGroup& OptionsGroup::addOptionItem( const Options::Option* option, Minecraft* minecraft ) {
|
||||
if(option->isBoolean())
|
||||
createToggle(option, minecraft);
|
||||
else if(option->isProgress())
|
||||
createProgressSlider(option, minecraft);
|
||||
else if(option->isInt())
|
||||
createStepSlider(option, minecraft);
|
||||
OptionsGroup& OptionsGroup::addOptionItem(OptionId optId, Minecraft* minecraft ) {
|
||||
auto option = minecraft->options.getOpt(optId);
|
||||
|
||||
if (option == nullptr) return *this;
|
||||
|
||||
// TODO: do a options key class to check it faster via dynamic_cast
|
||||
if (option->getStringId().find("options.key") != std::string::npos) createKey(optId, minecraft);
|
||||
else if (dynamic_cast<OptionBool*>(option)) createToggle(optId, minecraft);
|
||||
else if (dynamic_cast<OptionFloat*>(option)) createProgressSlider(optId, minecraft);
|
||||
else if (dynamic_cast<OptionInt*>(option)) createStepSlider(optId, minecraft);
|
||||
else if (dynamic_cast<OptionString*>(option)) createTextbox(optId, minecraft);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
void OptionsGroup::createToggle( const Options::Option* option, Minecraft* minecraft ) {
|
||||
// TODO: wrap this copypaste shit into templates
|
||||
|
||||
void OptionsGroup::createToggle(OptionId optId, Minecraft* minecraft ) {
|
||||
ImageDef def;
|
||||
|
||||
def.setSrc(IntRectangle(160, 206, 39, 20));
|
||||
def.name = "gui/touchgui.png";
|
||||
def.width = 39 * 0.7f;
|
||||
def.height = 20 * 0.7f;
|
||||
OptionButton* element = new OptionButton(option);
|
||||
|
||||
OptionButton* element = new OptionButton(optId);
|
||||
element->setImageDef(def, true);
|
||||
element->updateImage(&minecraft->options);
|
||||
std::string itemLabel = I18n::get(option->getCaptionId());
|
||||
|
||||
std::string itemLabel = I18n::get(minecraft->options.getOpt(optId)->getStringId());
|
||||
|
||||
OptionsItem* item = new OptionsItem(itemLabel, element);
|
||||
|
||||
addChild(item);
|
||||
setupPositions();
|
||||
}
|
||||
|
||||
void OptionsGroup::createProgressSlider(OptionId optId, Minecraft* minecraft ) {
|
||||
Slider* element = new SliderFloat(minecraft, optId);
|
||||
element->width = 100;
|
||||
element->height = 20;
|
||||
|
||||
std::string itemLabel = I18n::get(minecraft->options.getOpt(optId)->getStringId());
|
||||
OptionsItem* item = new OptionsItem(itemLabel, element);
|
||||
addChild(item);
|
||||
setupPositions();
|
||||
}
|
||||
|
||||
void OptionsGroup::createProgressSlider( const Options::Option* option, Minecraft* minecraft ) {
|
||||
Slider* element = new Slider(minecraft,
|
||||
option,
|
||||
minecraft->options.getProgrssMin(option),
|
||||
minecraft->options.getProgrssMax(option));
|
||||
void OptionsGroup::createStepSlider(OptionId optId, Minecraft* minecraft ) {
|
||||
Slider* element = new SliderInt(minecraft, optId);
|
||||
element->width = 100;
|
||||
element->height = 20;
|
||||
std::string itemLabel = I18n::get(option->getCaptionId());
|
||||
std::string itemLabel = I18n::get(minecraft->options.getOpt(optId)->getStringId());
|
||||
OptionsItem* item = new OptionsItem(itemLabel, element);
|
||||
addChild(item);
|
||||
setupPositions();
|
||||
}
|
||||
|
||||
void OptionsGroup::createStepSlider( const Options::Option* option, Minecraft* minecraft ) {
|
||||
// integer-valued option; use step slider
|
||||
std::vector<int> steps;
|
||||
// render distance was removed; fall through to other cases
|
||||
if(option == &Options::Option::DIFFICULTY) {
|
||||
steps.push_back(0);
|
||||
steps.push_back(1);
|
||||
steps.push_back(2);
|
||||
steps.push_back(3);
|
||||
} else if(option == &Options::Option::GUI_SCALE) {
|
||||
// slider order: small,normal,large,larger,auto
|
||||
steps.push_back(1);
|
||||
steps.push_back(2);
|
||||
steps.push_back(3);
|
||||
steps.push_back(4);
|
||||
steps.push_back(0);
|
||||
} else {
|
||||
// fallback: use single value; duplicate so numSteps>1 and avoid divide-by-zero
|
||||
steps.push_back(0);
|
||||
steps.push_back(0);
|
||||
}
|
||||
Slider* element = new Slider(minecraft, option, steps);
|
||||
void OptionsGroup::createTextbox(OptionId optId, Minecraft* minecraft) {
|
||||
TextBox* element = new TextOption(minecraft, optId);
|
||||
element->width = 100;
|
||||
element->height = 20;
|
||||
std::string itemLabel = I18n::get(option->getCaptionId());
|
||||
|
||||
std::string itemLabel = I18n::get(minecraft->options.getOpt(optId)->getStringId());
|
||||
OptionsItem* item = new OptionsItem(itemLabel, element);
|
||||
addChild(item);
|
||||
setupPositions();
|
||||
}
|
||||
|
||||
void OptionsGroup::createKey(OptionId optId, Minecraft* minecraft) {
|
||||
KeyOption* element = new KeyOption(minecraft, optId);
|
||||
element->width = 50;
|
||||
element->height = 20;
|
||||
|
||||
std::string itemLabel = I18n::get(minecraft->options.getOpt(optId)->getStringId());
|
||||
OptionsItem* item = new OptionsItem(itemLabel, element);
|
||||
addChild(item);
|
||||
setupPositions();
|
||||
}
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
#include <string>
|
||||
#include "GuiElementContainer.h"
|
||||
#include "ScrollingPane.h"
|
||||
#include "../../Options.h"
|
||||
|
||||
class Font;
|
||||
@@ -16,11 +17,15 @@ public:
|
||||
OptionsGroup(std::string labelID);
|
||||
virtual void setupPositions();
|
||||
virtual void render(Minecraft* minecraft, int xm, int ym);
|
||||
virtual OptionsGroup& addOptionItem(const Options::Option* option, Minecraft* minecraft);
|
||||
OptionsGroup& addOptionItem(OptionId optId, Minecraft* minecraft);
|
||||
protected:
|
||||
virtual void createToggle(const Options::Option* option, Minecraft* minecraft);
|
||||
virtual void createProgressSlider(const Options::Option* option, Minecraft* minecraft);
|
||||
virtual void createStepSlider(const Options::Option* option, Minecraft* minecraft);
|
||||
|
||||
void createToggle(OptionId optId, Minecraft* minecraft);
|
||||
void createProgressSlider(OptionId optId, Minecraft* minecraft);
|
||||
void createStepSlider(OptionId optId, Minecraft* minecraft);
|
||||
void createTextbox(OptionId optId, Minecraft* minecraft);
|
||||
void createKey(OptionId optId, Minecraft* minecraft);
|
||||
|
||||
std::string label;
|
||||
};
|
||||
|
||||
|
||||
@@ -1,64 +0,0 @@
|
||||
#include "OptionsPane.h"
|
||||
#include "OptionsGroup.h"
|
||||
#include "OptionsItem.h"
|
||||
#include "ImageButton.h"
|
||||
#include "Slider.h"
|
||||
#include "../../Minecraft.h"
|
||||
|
||||
OptionsPane::OptionsPane() {
|
||||
|
||||
}
|
||||
|
||||
void OptionsPane::setupPositions() {
|
||||
int currentHeight = y + 1;
|
||||
for(std::vector<GuiElement*>::iterator it = children.begin(); it != children.end(); ++it ) {
|
||||
(*it)->width = width;
|
||||
(*it)->y = currentHeight;
|
||||
(*it)->x = x;
|
||||
currentHeight += (*it)->height + 1;
|
||||
}
|
||||
height = currentHeight;
|
||||
super::setupPositions();
|
||||
}
|
||||
|
||||
OptionsGroup& OptionsPane::createOptionsGroup( std::string label ) {
|
||||
OptionsGroup* newGroup = new OptionsGroup(label);
|
||||
children.push_back(newGroup);
|
||||
// create and return a new group index
|
||||
return *newGroup;
|
||||
}
|
||||
|
||||
void OptionsPane::createToggle( unsigned int group, std::string label, const Options::Option* option ) {
|
||||
// if(group > children.size()) return;
|
||||
// ImageDef def;
|
||||
// def.setSrc(IntRectangle(160, 206, 39, 20));
|
||||
// def.name = "gui/touchgui.png";
|
||||
// def.width = 39 * 0.7f;
|
||||
// def.height = 20 * 0.7f;
|
||||
// OptionButton* element = new OptionButton(option);
|
||||
// element->setImageDef(def, true);
|
||||
// OptionsItem* item = new OptionsItem(label, element);
|
||||
// ((OptionsGroup*)children[group])->addChild(item);
|
||||
// setupPositions();
|
||||
}
|
||||
|
||||
void OptionsPane::createProgressSlider( Minecraft* minecraft, unsigned int group, std::string label, const Options::Option* option, float progressMin/*=1.0f*/, float progressMax/*=1.0f */ ) {
|
||||
// if(group > children.size()) return;
|
||||
// Slider* element = new Slider(minecraft, option, progressMin, progressMax);
|
||||
// element->width = 100;
|
||||
// element->height = 20;
|
||||
// OptionsItem* item = new OptionsItem(label, element);
|
||||
// ((OptionsGroup*)children[group])->addChild(item);
|
||||
// setupPositions();
|
||||
}
|
||||
|
||||
void OptionsPane::createStepSlider( Minecraft* minecraft, unsigned int group, std::string label, const Options::Option* option, const std::vector<int>& stepVec ) {
|
||||
// if(group > children.size()) return;
|
||||
// Slider* element = new Slider(minecraft, option, stepVec);
|
||||
// element->width = 100;
|
||||
// element->height = 20;
|
||||
// sliders.push_back(element);
|
||||
// OptionsItem* item = new OptionsItem(label, element);
|
||||
// ((OptionsGroup*)children[group])->addChild(item);
|
||||
// setupPositions();
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
#ifndef ITEMPANE_H__
|
||||
#define ITEMPANE_H__
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "GuiElementContainer.h"
|
||||
#include "../../../world/item/ItemInstance.h"
|
||||
#include "../../../client/Options.h"
|
||||
class Font;
|
||||
class Textures;
|
||||
class NinePatchLayer;
|
||||
class ItemPane;
|
||||
class OptionButton;
|
||||
class Button;
|
||||
class OptionsGroup;
|
||||
class Slider;
|
||||
class Minecraft;
|
||||
class OptionsPane: public GuiElementContainer
|
||||
{
|
||||
typedef GuiElementContainer super;
|
||||
public:
|
||||
OptionsPane();
|
||||
OptionsGroup& createOptionsGroup( std::string label );
|
||||
void createToggle( unsigned int group, std::string label, const Options::Option* option );
|
||||
void createProgressSlider(Minecraft* minecraft, unsigned int group, std::string label, const Options::Option* option, float progressMin=1.0f, float progressMax=1.0f );
|
||||
void createStepSlider(Minecraft* minecraft, unsigned int group, std::string label, const Options::Option* option, const std::vector<int>& stepVec );
|
||||
void setupPositions();
|
||||
};
|
||||
|
||||
#endif /*ITEMPANE_H__*/
|
||||
@@ -5,39 +5,8 @@
|
||||
#include "../../../util/Mth.h"
|
||||
#include <algorithm>
|
||||
#include <assert.h>
|
||||
Slider::Slider(Minecraft* minecraft, const Options::Option* option, float progressMin, float progressMax)
|
||||
: sliderType(SliderProgress), mouseDownOnElement(false), option(option), numSteps(0), progressMin(progressMin), progressMax(progressMax) {
|
||||
if(option != NULL) {
|
||||
percentage = (minecraft->options.getProgressValue(option) - progressMin) / (progressMax - progressMin);
|
||||
}
|
||||
}
|
||||
|
||||
Slider::Slider(Minecraft* minecraft, const Options::Option* option, const std::vector<int>& stepVec )
|
||||
: sliderType(SliderStep),
|
||||
curStepValue(0),
|
||||
curStep(0),
|
||||
sliderSteps(stepVec),
|
||||
mouseDownOnElement(false),
|
||||
option(option),
|
||||
percentage(0),
|
||||
progressMin(0.0f),
|
||||
progressMax(1.0) {
|
||||
assert(stepVec.size() > 1);
|
||||
numSteps = sliderSteps.size();
|
||||
if(option != NULL) {
|
||||
// initialize slider position based on the current option value
|
||||
curStepValue = minecraft->options.getIntValue(option);
|
||||
auto currentItem = std::find(sliderSteps.begin(), sliderSteps.end(), curStepValue);
|
||||
if(currentItem != sliderSteps.end()) {
|
||||
curStep = static_cast<int>(currentItem - sliderSteps.begin());
|
||||
} else {
|
||||
// fallback to first step
|
||||
curStep = 0;
|
||||
curStepValue = sliderSteps[0];
|
||||
}
|
||||
percentage = float(curStep) / float(numSteps - 1);
|
||||
}
|
||||
}
|
||||
Slider::Slider(OptionId optId) : m_mouseDownOnElement(false), m_optId(optId), m_numSteps(0) {}
|
||||
|
||||
void Slider::render( Minecraft* minecraft, int xm, int ym ) {
|
||||
int xSliderStart = x + 5;
|
||||
@@ -49,61 +18,71 @@ void Slider::render( Minecraft* minecraft, int xm, int ym ) {
|
||||
int barWidth = xSliderEnd - xSliderStart;
|
||||
//fill(x, y + 8, x + (int)(width * percentage), y + height, 0xffff00ff);
|
||||
fill(xSliderStart, ySliderStart, xSliderEnd, ySliderEnd, 0xff606060);
|
||||
if(sliderType == SliderStep) {
|
||||
// numSteps should be >=2; protect against bad input (zero division)
|
||||
if(numSteps <= 1) {
|
||||
// nothing to render
|
||||
} else {
|
||||
int stepDistance = barWidth / (numSteps -1);
|
||||
for(int a = 0; a <= numSteps - 1; ++a) {
|
||||
int renderSliderStepPosX = xSliderStart + a * stepDistance + 1;
|
||||
fill(renderSliderStepPosX - 1, ySliderStart - 2, renderSliderStepPosX + 1, ySliderEnd + 2, 0xff606060);
|
||||
}
|
||||
|
||||
if (m_numSteps > 2) {
|
||||
int stepDistance = barWidth / (m_numSteps-1);
|
||||
for(int a = 0; a <= m_numSteps; ++a) {
|
||||
int renderSliderStepPosX = xSliderStart + a * stepDistance + 1;
|
||||
fill(renderSliderStepPosX - 1, ySliderStart - 2, renderSliderStepPosX + 1, ySliderEnd + 2, 0xff606060);
|
||||
}
|
||||
}
|
||||
|
||||
minecraft->textures->loadAndBindTexture("gui/touchgui.png");
|
||||
blit(xSliderStart + (int)(percentage * barWidth) - handleSizeX / 2, y, 226, 126, handleSizeX, handleSizeY, handleSizeX, handleSizeY);
|
||||
blit(xSliderStart + (int)(m_percentage * barWidth) - handleSizeX / 2, y, 226, 126, handleSizeX, handleSizeY, handleSizeX, handleSizeY);
|
||||
}
|
||||
|
||||
void Slider::mouseClicked( Minecraft* minecraft, int x, int y, int buttonNum ) {
|
||||
if(pointInside(x, y)) {
|
||||
mouseDownOnElement = true;
|
||||
m_mouseDownOnElement = true;
|
||||
}
|
||||
}
|
||||
|
||||
void Slider::mouseReleased( Minecraft* minecraft, int x, int y, int buttonNum ) {
|
||||
mouseDownOnElement = false;
|
||||
if(sliderType == SliderStep) {
|
||||
curStep = Mth::floor((percentage * (numSteps-1) + 0.5f));
|
||||
curStepValue = sliderSteps[Mth::Min(curStep, numSteps-1)];
|
||||
percentage = float(curStep) / (numSteps - 1);
|
||||
setOption(minecraft);
|
||||
}
|
||||
}
|
||||
m_mouseDownOnElement = false;
|
||||
}
|
||||
|
||||
void Slider::tick(Minecraft* minecraft) {
|
||||
if(minecraft->screen != NULL) {
|
||||
int xm = Mouse::getX();
|
||||
int ym = Mouse::getY();
|
||||
|
||||
minecraft->screen->toGUICoordinate(xm, ym);
|
||||
if(mouseDownOnElement) {
|
||||
percentage = float(xm - x) / float(width);
|
||||
percentage = Mth::clamp(percentage, 0.0f, 1.0f);
|
||||
setOption(minecraft);
|
||||
|
||||
if(m_mouseDownOnElement) {
|
||||
m_percentage = float(xm - x) / float(width);
|
||||
m_percentage = Mth::clamp(m_percentage, 0.0f, 1.0f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Slider::setOption( Minecraft* minecraft ) {
|
||||
if(option != NULL) {
|
||||
if(sliderType == SliderStep) {
|
||||
if(minecraft->options.getIntValue(option) != curStepValue) {
|
||||
minecraft->options.set(option, curStepValue);
|
||||
}
|
||||
} else {
|
||||
if(minecraft->options.getProgressValue(option) != percentage * (progressMax - progressMin) + progressMin) {
|
||||
minecraft->options.set(option, percentage * (progressMax - progressMin) + progressMin);
|
||||
}
|
||||
}
|
||||
SliderFloat::SliderFloat(Minecraft* minecraft, OptionId option)
|
||||
: Slider(option), m_option(dynamic_cast<OptionFloat*>(minecraft->options.getOpt(option)))
|
||||
{
|
||||
m_percentage = Mth::clamp((m_option->get() - m_option->getMin()) / (m_option->getMax() - m_option->getMin()), 0.f, 1.f);
|
||||
}
|
||||
|
||||
SliderInt::SliderInt(Minecraft* minecraft, OptionId option)
|
||||
: Slider(option), m_option(dynamic_cast<OptionInt*>(minecraft->options.getOpt(option)))
|
||||
{
|
||||
m_numSteps = m_option->getMax() - m_option->getMin();
|
||||
m_percentage = float(m_option->get() - m_option->getMin()) / (m_numSteps-1);
|
||||
}
|
||||
|
||||
void SliderInt::mouseReleased( Minecraft* minecraft, int x, int y, int buttonNum ) {
|
||||
Slider::mouseReleased(minecraft, x, y, buttonNum);
|
||||
|
||||
if (pointInside(x, y)) {
|
||||
int curStep = Mth::floor(m_percentage * (m_numSteps-1));
|
||||
m_percentage = float(curStep - m_option->getMin()) / (m_numSteps-1);
|
||||
|
||||
minecraft->options.set(m_optId, curStep);
|
||||
}
|
||||
}
|
||||
|
||||
void SliderFloat::mouseReleased( Minecraft* minecraft, int x, int y, int buttonNum ) {
|
||||
Slider::mouseReleased(minecraft, x, y, buttonNum);
|
||||
|
||||
if (pointInside(x, y)) {
|
||||
minecraft->options.set(m_optId, m_percentage * (m_option->getMax() - m_option->getMin()) + m_option->getMin());
|
||||
}
|
||||
}
|
||||
@@ -3,38 +3,45 @@
|
||||
|
||||
#include "GuiElement.h"
|
||||
#include "../../../client/Options.h"
|
||||
enum SliderType {
|
||||
SliderProgress, // Sets slider between {0..1}
|
||||
SliderStep // Uses the closest step
|
||||
};
|
||||
#include <client/Option.h>
|
||||
|
||||
class Slider : public GuiElement {
|
||||
typedef GuiElement super;
|
||||
public:
|
||||
// Creates a progress slider with no steps
|
||||
Slider(Minecraft* minecraft, const Options::Option* option, float progressMin, float progressMax);
|
||||
Slider(Minecraft* minecraft, const Options::Option* option, const std::vector<int>& stepVec);
|
||||
virtual void render( Minecraft* minecraft, int xm, int ym );
|
||||
|
||||
virtual void mouseClicked( Minecraft* minecraft, int x, int y, int buttonNum );
|
||||
|
||||
virtual void mouseReleased( Minecraft* minecraft, int x, int y, int buttonNum );
|
||||
|
||||
virtual void tick(Minecraft* minecraft);
|
||||
|
||||
private:
|
||||
virtual void setOption(Minecraft* minecraft);
|
||||
|
||||
private:
|
||||
SliderType sliderType;
|
||||
std::vector<int> sliderSteps;
|
||||
bool mouseDownOnElement;
|
||||
float percentage;
|
||||
int curStepValue;
|
||||
int curStep;
|
||||
int numSteps;
|
||||
float progressMin;
|
||||
float progressMax;
|
||||
const Options::Option* option;
|
||||
protected:
|
||||
Slider(OptionId optId);
|
||||
|
||||
OptionId m_optId;
|
||||
|
||||
bool m_mouseDownOnElement;
|
||||
float m_percentage;
|
||||
int m_numSteps;
|
||||
};
|
||||
|
||||
class SliderFloat : public Slider {
|
||||
public:
|
||||
SliderFloat(Minecraft* minecraft, OptionId option);
|
||||
|
||||
virtual void mouseReleased( Minecraft* minecraft, int x, int y, int buttonNum ) override;
|
||||
|
||||
protected:
|
||||
OptionFloat* m_option;
|
||||
};
|
||||
|
||||
|
||||
class SliderInt : public Slider {
|
||||
public:
|
||||
SliderInt(Minecraft* minecraft, OptionId option);
|
||||
|
||||
virtual void mouseReleased( Minecraft* minecraft, int x, int y, int buttonNum ) override;
|
||||
|
||||
protected:
|
||||
OptionInt* m_option;
|
||||
};
|
||||
|
||||
#endif /*NET_MINECRAFT_CLIENT_GUI_COMPONENTS__Slider_H__*/
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
#include "SmallButton.h"
|
||||
|
||||
SmallButton::SmallButton( int id, int x, int y, const std::string& msg )
|
||||
: super(id, x, y, 150, 20, msg),
|
||||
option(NULL)
|
||||
{
|
||||
}
|
||||
|
||||
SmallButton::SmallButton( int id, int x, int y, int width, int height, const std::string& msg )
|
||||
: super(id, x, y, width, height, msg),
|
||||
option(NULL)
|
||||
{
|
||||
}
|
||||
|
||||
SmallButton::SmallButton( int id, int x, int y, Options::Option* item, const std::string& msg )
|
||||
: super(id, x, y, 150, 20, msg),
|
||||
option(item)
|
||||
{
|
||||
}
|
||||
|
||||
Options::Option* SmallButton::getOption()
|
||||
{
|
||||
return option;
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
#ifndef NET_MINECRAFT_CLIENT_GUI_COMPONENTS__SmallButton_H__
|
||||
#define NET_MINECRAFT_CLIENT_GUI_COMPONENTS__SmallButton_H__
|
||||
|
||||
//package net.minecraft.client.gui;
|
||||
|
||||
#include <string>
|
||||
#include "Button.h"
|
||||
#include "../../Options.h"
|
||||
|
||||
class SmallButton: public Button
|
||||
{
|
||||
typedef Button super;
|
||||
public:
|
||||
SmallButton(int id, int x, int y, const std::string& msg);
|
||||
SmallButton(int id, int x, int y, int width, int height, const std::string& msg);
|
||||
SmallButton(int id, int x, int y, Options::Option* item, const std::string& msg);
|
||||
|
||||
Options::Option* getOption();
|
||||
private:
|
||||
Options::Option* option;
|
||||
};
|
||||
|
||||
#endif /*NET_MINECRAFT_CLIENT_GUI_COMPONENTS__SmallButton_H__*/
|
||||
@@ -49,13 +49,13 @@ void TextBox::mouseClicked(Minecraft* minecraft, int x, int y, int buttonNum) {
|
||||
}
|
||||
}
|
||||
|
||||
void TextBox::handleChar(char c) {
|
||||
void TextBox::charPressed(Minecraft* minecraft, char c) {
|
||||
if (focused && c >= 32 && c < 127 && (int)text.size() < 256) {
|
||||
text.push_back(c);
|
||||
}
|
||||
}
|
||||
|
||||
void TextBox::handleKey(int key) {
|
||||
void TextBox::keyPressed(Minecraft* minecraft, int key) {
|
||||
if (focused && key == Keyboard::KEY_BACKSPACE && !text.empty()) {
|
||||
text.pop_back();
|
||||
}
|
||||
|
||||
@@ -26,8 +26,8 @@ public:
|
||||
|
||||
virtual void render(Minecraft* minecraft, int xm, int ym);
|
||||
|
||||
virtual void handleKey(int key);
|
||||
virtual void handleChar(char c);
|
||||
virtual void keyPressed(Minecraft* minecraft, int key);
|
||||
virtual void charPressed(Minecraft* minecraft, char c);
|
||||
virtual void tick(Minecraft* minecraft);
|
||||
|
||||
public:
|
||||
|
||||
17
src/client/gui/components/TextOption.cpp
Normal file
17
src/client/gui/components/TextOption.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include "TextOption.h"
|
||||
#include <client/Minecraft.h>
|
||||
|
||||
TextOption::TextOption(Minecraft* minecraft, OptionId optId)
|
||||
: TextBox((int)optId, minecraft->options.getOpt(optId)->getStringId())
|
||||
{
|
||||
text = minecraft->options.getStringValue(optId);
|
||||
}
|
||||
|
||||
bool TextOption::loseFocus(Minecraft* minecraft) {
|
||||
if (TextBox::loseFocus(minecraft)) {
|
||||
minecraft->options.set((OptionId)id, text);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
10
src/client/gui/components/TextOption.h
Normal file
10
src/client/gui/components/TextOption.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
#include "TextBox.h"
|
||||
#include <client/Options.h>
|
||||
|
||||
class TextOption : public TextBox {
|
||||
public:
|
||||
TextOption(Minecraft* minecraft, OptionId optId);
|
||||
|
||||
virtual bool loseFocus(Minecraft* minecraft);
|
||||
};
|
||||
Reference in New Issue
Block a user