2012-06-26 20:23:05 +00:00
|
|
|
// * This file is part of the COLOBOT source code
|
|
|
|
// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch
|
2012-09-10 21:29:38 +00:00
|
|
|
// * Copyright (C) 2012, Polish Portal of Colobot (PPC)
|
2012-06-26 20:23:05 +00:00
|
|
|
// *
|
|
|
|
// * This program is free software: you can redistribute it and/or modify
|
|
|
|
// * it under the terms of the GNU General Public License as published by
|
|
|
|
// * the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// * (at your option) any later version.
|
|
|
|
// *
|
|
|
|
// * This program is distributed in the hope that it will be useful,
|
|
|
|
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// * GNU General Public License for more details.
|
|
|
|
// *
|
|
|
|
// * You should have received a copy of the GNU General Public License
|
|
|
|
// * along with this program. If not, see http://www.gnu.org/licenses/.
|
|
|
|
|
|
|
|
// editvalue.cpp
|
|
|
|
|
|
|
|
|
|
|
|
#include "common/event.h"
|
|
|
|
#include "common/misc.h"
|
|
|
|
#include "common/iman.h"
|
|
|
|
#include "ui/edit.h"
|
|
|
|
#include "ui/button.h"
|
|
|
|
#include "ui/editvalue.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-09-10 21:29:38 +00:00
|
|
|
namespace Ui {
|
2012-06-26 20:23:05 +00:00
|
|
|
// Object's constructor.
|
|
|
|
|
2012-09-10 21:29:38 +00:00
|
|
|
//CEditValue::CEditValue(CInstanceManager* iMan) : CControl(iMan)
|
|
|
|
CEditValue::CEditValue() : CControl ()
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
|
|
|
m_edit = 0;
|
|
|
|
m_buttonUp = 0;
|
|
|
|
m_buttonDown = 0;
|
|
|
|
|
|
|
|
m_type = EVT_100; // %
|
|
|
|
m_stepValue = 0.1f; // 10%
|
|
|
|
m_minValue = 0.0f; // 0%
|
|
|
|
m_maxValue = 1.0f; // 100%
|
|
|
|
}
|
|
|
|
|
|
|
|
// Object's destructor.
|
|
|
|
|
|
|
|
CEditValue::~CEditValue()
|
|
|
|
{
|
|
|
|
delete m_edit;
|
|
|
|
delete m_buttonUp;
|
|
|
|
delete m_buttonDown;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Creates a new button.
|
|
|
|
|
2012-09-10 21:29:38 +00:00
|
|
|
bool CEditValue::Create(Math::Point pos, Math::Point dim, int icon, EventType eventType)
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
2012-09-10 21:29:38 +00:00
|
|
|
Ui::CEdit* pe;
|
|
|
|
Ui::CButton* pc;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
2012-09-10 21:29:38 +00:00
|
|
|
if ( eventType == EVENT_NULL ) eventType = GetUniqueEventType();
|
|
|
|
CControl::Create(pos, dim, icon, eventType);
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
|
|
GlintDelete();
|
|
|
|
|
2012-09-10 21:29:38 +00:00
|
|
|
m_edit = new Ui::CEdit();
|
|
|
|
pe = static_cast<Ui::CEdit*>(m_edit);
|
2012-06-26 20:23:05 +00:00
|
|
|
pe->Create(pos, dim, 0, EVENT_NULL);
|
|
|
|
pe->SetMaxChar(4);
|
|
|
|
|
2012-09-10 21:29:38 +00:00
|
|
|
m_buttonUp = new Ui::CButton();
|
|
|
|
pc = static_cast<Ui::CButton*>(m_buttonUp);
|
2012-06-26 20:23:05 +00:00
|
|
|
pc->Create(pos, dim, 49, EVENT_NULL); // ^
|
|
|
|
pc->SetRepeat(true);
|
|
|
|
|
2012-09-10 21:29:38 +00:00
|
|
|
m_buttonDown = new Ui::CButton();
|
|
|
|
pc = static_cast<Ui::CButton*>(m_buttonDown);
|
2012-06-26 20:23:05 +00:00
|
|
|
pc->Create(pos, dim, 50, EVENT_NULL); // v
|
|
|
|
pc->SetRepeat(true);
|
|
|
|
|
|
|
|
MoveAdjust();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CEditValue::SetPos(Math::Point pos)
|
|
|
|
{
|
|
|
|
CControl::SetPos(pos);
|
|
|
|
MoveAdjust();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CEditValue::SetDim(Math::Point dim)
|
|
|
|
{
|
|
|
|
CControl::SetDim(dim);
|
|
|
|
MoveAdjust();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CEditValue::MoveAdjust()
|
|
|
|
{
|
|
|
|
Math::Point pos, dim;
|
|
|
|
|
|
|
|
if ( m_edit != 0 )
|
|
|
|
{
|
|
|
|
pos.x = m_pos.x;
|
|
|
|
pos.y = m_pos.y;
|
|
|
|
dim.x = m_dim.x-m_dim.y*0.6f;
|
|
|
|
dim.y = m_dim.y;
|
|
|
|
m_edit->SetPos(pos);
|
|
|
|
m_edit->SetDim(dim);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( m_buttonUp != 0 )
|
|
|
|
{
|
|
|
|
pos.x = m_pos.x+m_dim.x-m_dim.y*0.6f;
|
|
|
|
pos.y = m_pos.y+m_dim.y*0.5f;
|
|
|
|
dim.x = m_dim.y*0.6f;
|
|
|
|
dim.y = m_dim.y*0.5f;
|
|
|
|
m_buttonUp->SetPos(pos);
|
|
|
|
m_buttonUp->SetDim(dim);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( m_buttonDown != 0 )
|
|
|
|
{
|
|
|
|
pos.x = m_pos.x+m_dim.x-m_dim.y*0.6f;
|
|
|
|
pos.y = m_pos.y;
|
|
|
|
dim.x = m_dim.y*0.6f;
|
|
|
|
dim.y = m_dim.y*0.5f;
|
|
|
|
m_buttonDown->SetPos(pos);
|
|
|
|
m_buttonDown->SetDim(dim);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Management of an event.
|
|
|
|
|
|
|
|
bool CEditValue::EventProcess(const Event &event)
|
|
|
|
{
|
|
|
|
float value;
|
|
|
|
|
|
|
|
CControl::EventProcess(event);
|
|
|
|
|
|
|
|
if ( (m_state & STATE_VISIBLE) == 0 ) return true;
|
|
|
|
if ( (m_state & STATE_ENABLE) == 0 ) return true;
|
|
|
|
|
|
|
|
if ( m_edit != 0 )
|
|
|
|
{
|
2012-09-10 21:29:38 +00:00
|
|
|
if ( m_edit->GetFocus() &&
|
|
|
|
event.type == EVENT_KEY_DOWN &&
|
2012-09-19 20:17:28 +00:00
|
|
|
event.key.key == KEY(RETURN) )
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
2012-09-10 21:29:38 +00:00
|
|
|
value = GetValue();
|
2012-06-26 20:23:05 +00:00
|
|
|
if ( value > m_maxValue ) value = m_maxValue;
|
|
|
|
if ( value < m_minValue ) value = m_minValue;
|
|
|
|
SetValue(value, true);
|
|
|
|
HiliteValue(event);
|
|
|
|
}
|
|
|
|
if ( !m_edit->EventProcess(event) ) return false;
|
|
|
|
|
2012-09-10 21:29:38 +00:00
|
|
|
if ( event.type == m_edit->GetEventType() )
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
2012-09-10 21:29:38 +00:00
|
|
|
Event newEvent(m_eventType);
|
2012-06-26 20:23:05 +00:00
|
|
|
m_event->AddEvent(newEvent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( m_buttonUp != 0 )
|
|
|
|
{
|
2012-09-10 21:29:38 +00:00
|
|
|
if ( event.type == m_buttonUp->GetEventType() )
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
2012-09-10 21:29:38 +00:00
|
|
|
value = GetValue()+m_stepValue;
|
2012-06-26 20:23:05 +00:00
|
|
|
if ( value > m_maxValue ) value = m_maxValue;
|
|
|
|
SetValue(value, true);
|
|
|
|
HiliteValue(event);
|
|
|
|
}
|
|
|
|
if ( !m_buttonUp->EventProcess(event) ) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( m_buttonDown != 0 )
|
|
|
|
{
|
2012-09-10 21:29:38 +00:00
|
|
|
if ( event.type == m_buttonDown->GetEventType() )
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
2012-09-10 21:29:38 +00:00
|
|
|
value = GetValue()-m_stepValue;
|
2012-06-26 20:23:05 +00:00
|
|
|
if ( value < m_minValue ) value = m_minValue;
|
|
|
|
SetValue(value, true);
|
|
|
|
HiliteValue(event);
|
|
|
|
}
|
|
|
|
if ( !m_buttonDown->EventProcess(event) ) return false;
|
|
|
|
}
|
|
|
|
|
2012-09-19 19:45:41 +00:00
|
|
|
if (event.type == EVENT_MOUSE_WHEEL &&
|
|
|
|
event.mouseWheel.dir == WHEEL_UP &&
|
|
|
|
Detect(event.mouseWheel.pos))
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
2012-09-10 21:29:38 +00:00
|
|
|
value = GetValue()+m_stepValue;
|
2012-06-26 20:23:05 +00:00
|
|
|
if ( value > m_maxValue ) value = m_maxValue;
|
|
|
|
SetValue(value, true);
|
|
|
|
HiliteValue(event);
|
|
|
|
}
|
2012-09-10 21:29:38 +00:00
|
|
|
if ( event.type == EVENT_KEY_DOWN &&
|
2012-09-19 19:45:41 +00:00
|
|
|
event.mouseWheel.dir == WHEEL_DOWN &&
|
|
|
|
Detect(event.mouseWheel.pos))
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
2012-09-10 21:29:38 +00:00
|
|
|
value = GetValue()-m_stepValue;
|
2012-06-26 20:23:05 +00:00
|
|
|
if ( value < m_minValue ) value = m_minValue;
|
|
|
|
SetValue(value, true);
|
|
|
|
HiliteValue(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Puts in evidence the edited value.
|
|
|
|
|
|
|
|
void CEditValue::HiliteValue(const Event &event)
|
|
|
|
{
|
|
|
|
int pos;
|
|
|
|
|
|
|
|
if ( m_edit == 0 ) return;
|
|
|
|
|
2012-09-10 21:29:38 +00:00
|
|
|
pos = m_edit->GetTextLength();
|
2012-06-26 20:23:05 +00:00
|
|
|
if ( m_type == EVT_100 && pos > 0 )
|
|
|
|
{
|
|
|
|
pos --; // not only selects the "%"
|
|
|
|
}
|
|
|
|
|
|
|
|
m_edit->SetCursor(pos, 0);
|
|
|
|
m_edit->SetFocus(true);
|
|
|
|
|
|
|
|
Event newEvent = event;
|
2012-09-10 21:29:38 +00:00
|
|
|
newEvent.type = EVENT_ACTIVE;
|
|
|
|
newEvent.active.gain = true; // TODO not much pretty sure about it
|
|
|
|
newEvent.param = m_edit->GetEventType();
|
2012-06-26 20:23:05 +00:00
|
|
|
m_event->AddEvent(newEvent); // defocus the other objects
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Draw button.
|
|
|
|
|
|
|
|
void CEditValue::Draw()
|
|
|
|
{
|
|
|
|
if ( (m_state & STATE_VISIBLE) == 0 ) return;
|
|
|
|
|
|
|
|
if ( m_state & STATE_SHADOW )
|
|
|
|
{
|
|
|
|
DrawShadow(m_pos, m_dim);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( m_edit != 0 )
|
|
|
|
{
|
|
|
|
m_edit->Draw();
|
|
|
|
}
|
|
|
|
if ( m_buttonUp != 0 )
|
|
|
|
{
|
|
|
|
m_buttonUp->Draw();
|
|
|
|
}
|
|
|
|
if ( m_buttonDown != 0 )
|
|
|
|
{
|
|
|
|
m_buttonDown->Draw();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Choosing the type of value.
|
|
|
|
|
|
|
|
void CEditValue::SetType(EditValueType type)
|
|
|
|
{
|
|
|
|
m_type = type;
|
|
|
|
}
|
|
|
|
|
2012-09-10 21:29:38 +00:00
|
|
|
EditValueType CEditValue::GetType()
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
|
|
|
return m_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Changes the value.
|
|
|
|
|
|
|
|
void CEditValue::SetValue(float value, bool bSendMessage)
|
|
|
|
{
|
|
|
|
char text[100];
|
|
|
|
|
|
|
|
if ( m_edit == 0 ) return;
|
|
|
|
|
|
|
|
text[0] = 0;
|
|
|
|
|
|
|
|
if ( m_type == EVT_INT )
|
|
|
|
{
|
2012-09-18 20:33:28 +00:00
|
|
|
sprintf(text, "%d", static_cast<int>(value));
|
2012-06-26 20:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( m_type == EVT_FLOAT )
|
|
|
|
{
|
|
|
|
sprintf(text, "%.2f", value);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( m_type == EVT_100 )
|
|
|
|
{
|
2012-09-18 20:33:28 +00:00
|
|
|
sprintf(text, "%d%%", static_cast<int>(value*100.0f));
|
2012-06-26 20:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_edit->SetText(text);
|
|
|
|
|
|
|
|
if ( bSendMessage )
|
|
|
|
{
|
2012-09-10 21:29:38 +00:00
|
|
|
Event newEvent(m_eventType);
|
2012-06-26 20:23:05 +00:00
|
|
|
m_event->AddEvent(newEvent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the edited value.
|
|
|
|
|
2012-09-10 21:29:38 +00:00
|
|
|
float CEditValue::GetValue()
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
|
|
|
char text[100];
|
2012-09-18 20:33:28 +00:00
|
|
|
float value = 0.0f;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
2012-09-18 20:33:28 +00:00
|
|
|
if ( m_edit != 0 )
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
2012-09-18 20:33:28 +00:00
|
|
|
m_edit->GetText(text, 100);
|
|
|
|
sscanf(text, "%f", &value);
|
2012-06-26 20:23:05 +00:00
|
|
|
|
2012-09-18 20:33:28 +00:00
|
|
|
if ( m_type == EVT_100 )
|
|
|
|
{
|
|
|
|
value = (value+0.5f)/100.0f;
|
|
|
|
if ( value < 0.01f ) value = 0.0f; // less than 1%?
|
|
|
|
}
|
|
|
|
}
|
2012-06-26 20:23:05 +00:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Management not for buttons.
|
|
|
|
|
|
|
|
void CEditValue::SetStepValue(float value)
|
|
|
|
{
|
|
|
|
m_stepValue = value;
|
|
|
|
}
|
|
|
|
|
2012-09-10 21:29:38 +00:00
|
|
|
float CEditValue::GetStepValue()
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
|
|
|
return m_stepValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Management of the minimum value.
|
|
|
|
|
|
|
|
void CEditValue::SetMinValue(float value)
|
|
|
|
{
|
|
|
|
m_minValue = value;
|
|
|
|
}
|
|
|
|
|
2012-09-10 21:29:38 +00:00
|
|
|
float CEditValue::GetMinValue()
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
|
|
|
return m_minValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Management of the maximum value.
|
|
|
|
|
|
|
|
void CEditValue::SetMaxValue(float value)
|
|
|
|
{
|
|
|
|
m_maxValue = value;
|
|
|
|
}
|
|
|
|
|
2012-09-10 21:29:38 +00:00
|
|
|
float CEditValue::GetMaxValue()
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
|
|
|
return m_maxValue;
|
|
|
|
}
|
|
|
|
|
2012-09-10 21:29:38 +00:00
|
|
|
}
|