libsegl/glmenu/menuitems.cpp

200 lines
3.4 KiB
C++

#include "menuitems.h"
// MISendSDLEvent
MISendSDLEvent::MISendSDLEvent(std::string str, SDL_Event event) : MenuItem(str) {
resetEvent(event);
}
void MISendSDLEvent::select() {
SDL_PushEvent(&sendevent);
}
void MISendSDLEvent::resetEvent(SDL_Event event) {
sendevent = event;
}
// MITextLabel
MITextLabel::MITextLabel(std::string str) : MenuItem(str) {
selectable = false;
}
// MIValueLabel
MIValueLabel::MIValueLabel(std::string str) : MenuItem(str) {
selectable = false;
usevalue = true;
}
void MIValueLabel::setValue(std::string _val) {
value = _val;
}
// MIToggle
MIToggle::MIToggle(std::string str) : MenuItem(str) {
usevalue = true;
togglepos = 0;
}
void MIToggle::left() {
togglepos--;
if(togglepos<0)
togglepos = toggles.size()-1;
value = getValueString();
}
void MIToggle::right() {
togglepos++;
if((unsigned int)togglepos>=toggles.size())
togglepos = 0;
value = getValueString();
}
void MIToggle::select() {
right();
}
void MIToggle::addToggle(std::string str) {
toggles.push_back(str);
if(toggles.size()==1) {
value = getValueString();
}
}
int MIToggle::getMaxValueLen() {
int ret=0;
for(unsigned int i=0; i<toggles.size(); i++) {
ret = std::max(ret, (int)toggles[i].length());
}
return ret;
}
int MIToggle::getValueInt() {
return togglepos;
}
void MIToggle::setValueInt(int val) {
if(val<0||(unsigned int)val>=toggles.size())
return;
togglepos = val;
value = getValueString();
}
void MIToggle::setValueString(std::string str) {
for(unsigned int t=0; t<toggles.size(); t++) {
std::cout << str << " == " << toggles[t] << std::endl;
if(str==toggles[t]) {
togglepos = t;
value = getValueString();
return;
}
}
}
std::string MIToggle::getValueString() {
if(toggles.size()==0)
return "";
return toggles[togglepos];
}
// MIStringInput
MIStringInput::MIStringInput(std::string str, int _maxlen) : MenuItem(str) {
usevalue = true;
maxlen = _maxlen;
}
void MIStringInput::charInput(char c) {
// std::cout << "INPUT: " << c << ", maxlen" << maxlen << std::endl;
if(c=='\b') {
if(value.length()>0) {
value = value.substr(0, value.length()-1);
}
} else if(c>=32) {
if(!maxlen || value.length()<(unsigned int)maxlen)
value +=c;
// std::cout << maxlen << " && " << value.length() << " < " << maxlen << std::endl;
}
}
void MIStringInput::setValue(std::string str) {
value = str;
}
int MIStringInput::getMaxValueLen() {
return maxlen;
}
std::string MIStringInput::getValue() {
return value;
}
// MICheckbox
MICheckBox::MICheckBox(std::string str) : MenuItem(str) {
usevalue = true;
}
bool MICheckBox::isChecked() {
return state;
}
void MICheckBox::setState(bool _state) {
state = _state;
updateValue();
}
void MICheckBox::select() {
right();
}
void MICheckBox::right() {
state = !state;
updateValue();
}
void MICheckBox::left() {
state = !state;
updateValue();
}
int MICheckBox::getMaxValueLen() {
return 3;
}
void MICheckBox::updateValue() {
value = state ? "An" : "Aus";
}
// MIEventOnToggle
MIEventOnToggle::MIEventOnToggle(std::string str, SDL_Event event) : MIToggle(str) {
resetEvent(event);
}
void MIEventOnToggle::sendEvent() {
SDL_PushEvent(&sendevent);
}
void MIEventOnToggle::select() {
MIToggle::select();
sendEvent();
}
void MIEventOnToggle::left() {
MIToggle::left();
sendEvent();
}
void MIEventOnToggle::right() {
MIToggle::right();
sendEvent();
}
void MIEventOnToggle::resetEvent(SDL_Event event) {
sendevent = event;
}