colobot/colobot-base/ui/controls/color.cpp

191 lines
4.2 KiB
C++

/*
* This file is part of the Colobot: Gold Edition source code
* Copyright (C) 2001-2023, Daniel Roux, EPSITEC SA & TerranovaTeam
* http://epsitec.ch; http://colobot.info; http://github.com/colobot
*
* 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://gnu.org/licenses
*/
#include "ui/controls/color.h"
#include "common/event.h"
#include "common/restext.h"
#include "graphics/core/renderers.h"
#include "graphics/core/transparency.h"
#include "graphics/engine/engine.h"
#include <string.h>
namespace Ui
{
const float DELAY1 = 0.4f;
const float DELAY2 = 0.1f;
// Object's constructor.
CColor::CColor() : CControl()
{
m_bRepeat = false;
m_repeat = 0.0f;
m_color.r = 0.0f;
m_color.g = 0.0f;
m_color.b = 0.0f;
m_color.a = 0.0f;
}
// Object's destructor.
CColor::~CColor()
{
}
// Creates a new button.
bool CColor::Create(const glm::vec2& pos, const glm::vec2& dim, int icon, EventType eventType)
{
if ( eventType == EVENT_NULL ) eventType = GetUniqueEventType();
CControl::Create(pos, dim, icon, eventType);
if ( icon == -1 )
{
std::string name = GetResourceName(eventType);
SetName(name);
}
return true;
}
// Management of an event.
bool CColor::EventProcess(const Event &event)
{
if ( m_state & STATE_DEAD ) return true;
CControl::EventProcess(event);
if ( event.type == EVENT_FRAME && m_bRepeat )
{
if ( m_repeat != 0.0f )
{
m_repeat -= event.rTime;
if ( m_repeat <= 0.0f )
{
m_repeat = DELAY2;
m_event->AddEvent(Event(m_eventType));
return false;
}
}
}
if ( event.type == EVENT_MOUSE_BUTTON_DOWN &&
event.GetData<MouseButtonEventData>()->button == MOUSE_BUTTON_LEFT &&
(m_state & STATE_VISIBLE) &&
(m_state & STATE_ENABLE) )
{
if ( CControl::Detect(event.mousePos) )
{
m_repeat = DELAY1;
m_event->AddEvent(Event(m_eventType));
return false;
}
}
if (event.type == EVENT_MOUSE_BUTTON_UP &&
event.GetData<MouseButtonEventData>()->button == MOUSE_BUTTON_LEFT)
{
m_repeat = 0.0f;
}
return true;
}
// Draw the button.
void CColor::Draw()
{
Gfx::Color color;
glm::vec2 p1, p2;
if ( (m_state & STATE_VISIBLE) == 0 ) return;
if ( m_state & STATE_SHADOW )
{
DrawShadow(m_pos, m_dim);
}
auto renderer = m_engine->GetUIRenderer();
auto texture = m_engine->LoadTexture("textures/interface/button1.png");
renderer->SetTexture(texture);
renderer->SetTransparency(Gfx::TransparencyMode::NONE);
CControl::Draw();
p1.x = m_pos.x + (3.0f / 640.0f);
p1.y = m_pos.y + (3.0f / 480.0f);
p2.x = m_pos.x + m_dim.x - (3.0f / 640.0f);
p2.y = m_pos.y + m_dim.y - (3.0f / 480.0f);
color = GetColor();
glm::u8vec4 col = { color.r * 255, color.g * 255, color.b * 255, color.a * 255 };
renderer->SetTexture(Gfx::Texture{0});
renderer->SetTransparency(Gfx::TransparencyMode::NONE);
auto vertex = renderer->BeginPrimitive(Gfx::PrimitiveType::TRIANGLE_STRIP, 4);
vertex[0] = { { p1.x, p1.y }, {}, col };
vertex[1] = { { p1.x, p2.y }, {}, col };
vertex[2] = { { p2.x, p1.y }, {}, col };
vertex[3] = { { p2.x, p2.y }, {}, col };
renderer->EndPrimitive();
m_engine->AddStatisticTriangle(2);
}
void CColor::SetRepeat(bool bRepeat)
{
m_bRepeat = bRepeat;
}
bool CColor::GetRepeat()
{
return m_bRepeat;
}
void CColor::SetColor(Gfx::Color color)
{
m_color = color;
}
Gfx::Color CColor::GetColor()
{
return m_color;
}
}