colobot/src/ui/image.cpp

155 lines
3.3 KiB
C++
Raw Normal View History

/*
* This file is part of the Colobot: Gold Edition source code
* Copyright (C) 2001-2014, Daniel Roux, EPSITEC SA & TerranovaTeam
* http://epsiteс.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/image.h"
#include "common/event.h"
#include "common/misc.h"
#include "common/restext.h"
#include "graphics/engine/engine.h"
#include <string.h>
2012-09-18 22:04:21 +00:00
#include <stdio.h>
2015-08-02 09:40:47 +00:00
namespace Ui
{
// Object's constructor.
2012-09-10 21:29:38 +00:00
CImage::CImage() : CControl()
{
m_filename[0] = 0;
}
// Object's destructor.
CImage::~CImage()
{
if ( m_filename[0] != 0 )
{
2012-09-10 21:29:38 +00:00
m_engine->DeleteTexture(m_filename);
}
}
// Creates a new button.
2012-09-10 21:29:38 +00:00
bool CImage::Create(Math::Point pos, Math::Point dim, int icon, EventType eventType)
{
2012-09-10 21:29:38 +00:00
if ( eventType == EVENT_NULL ) eventType = GetUniqueEventType();
2012-09-10 21:29:38 +00:00
CControl::Create(pos, dim, icon, eventType);
if ( icon == -1 )
{
std::string name = GetResourceName(eventType);
SetName(name);
}
return true;
}
// Specifies the name of the image display.
void CImage::SetFilenameImage(const char *name)
{
if ( m_filename[0] != 0 )
{
2012-09-10 21:29:38 +00:00
m_engine->DeleteTexture(m_filename);
}
strcpy(m_filename, name);
}
2012-09-10 21:29:38 +00:00
char* CImage::GetFilenameImage()
{
return m_filename;
}
// Management of an event.
bool CImage::EventProcess(const Event &event)
{
return true;
}
// Draws button.
void CImage::Draw()
{
Math::Point uv1,uv2, corner, pos, dim;
float dp;
if ( (m_state & STATE_VISIBLE) == 0 ) return;
if ( m_state & STATE_SHADOW )
{
DrawShadow(m_pos, m_dim);
}
dp = 0.5f/256.0f;
if ( m_icon == 0 ) // hollow frame?
{
m_engine->SetTexture("textures/interface/button2.png");
2012-09-10 21:29:38 +00:00
m_engine->SetState(Gfx::ENG_RSTATE_NORMAL);
uv1.x = 160.0f / 256.0f;
uv1.y = 192.0f / 256.0f; // u-v texture
uv2.x = 192.0f / 256.0f;
uv2.y = 224.0f / 256.0f;
uv1.x += dp;
uv1.y += dp;
uv2.x -= dp;
uv2.y -= dp;
2012-09-10 21:29:38 +00:00
corner.x = 10.0f / 640.0f;
corner.y = 10.0f / 480.0f;
DrawIcon(m_pos, m_dim, uv1, uv2, corner, 8.0f/256.0f);
}
if ( m_filename[0] != 0 ) // displays an image?
{
std::string texFilename = m_filename;
texFilename = "textures/"+texFilename;
m_engine->LoadTexture(texFilename.c_str());
m_engine->SetTexture(texFilename);
2012-09-10 21:29:38 +00:00
m_engine->SetState(Gfx::ENG_RSTATE_NORMAL);
pos = m_pos;
dim = m_dim;
2012-09-10 21:29:38 +00:00
pos.x += 5.0f / 640.0f;
pos.y += 5.0f / 480.0f;
dim.x -= 10.0f / 640.0f;
dim.y -= 10.0f / 480.0f;
uv1.x = 0.0f;
uv1.y = 0.0f;
uv2.x = 1.0f;
uv2.y = 1.0f;
DrawIcon(pos, dim, uv1, uv2);
}
}
2012-09-10 21:29:38 +00:00
}