worldspawn/plugins/entity/colour.h

127 lines
2.6 KiB
C
Raw Normal View History

2020-11-17 11:16:16 +00:00
/*
Copyright (C) 2001-2006, William Joseph.
All Rights Reserved.
This file is part of GtkRadiant.
GtkRadiant 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 2 of the License, or
(at your option) any later version.
GtkRadiant 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 GtkRadiant; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#if !defined( INCLUDED_COLOUR_H )
#define INCLUDED_COLOUR_H
#include "ientity.h"
#include "irender.h"
#include "math/vector.h"
#include "eclasslib.h"
#include "generic/callback.h"
#include "stringio.h"
inline void default_colour(Vector3 &colour)
{
2021-08-04 20:23:18 +00:00
colour = Vector3(1, 1, 1);
2020-11-17 11:16:16 +00:00
}
inline void read_colour(Vector3 &colour, const char *value)
{
2021-08-04 20:23:18 +00:00
if (!string_parse_vector3(value, colour)) {
default_colour(colour);
}
2020-11-17 11:16:16 +00:00
}
inline void read_colour255(Vector3 &colour, const char *value)
{
2021-08-04 20:23:18 +00:00
if (!string_parse_vector3(value, colour)) {
default_colour(colour);
} else {
colour[0] /= 255;
colour[1] /= 255;
colour[2] /= 255;
}
2020-11-17 11:16:16 +00:00
}
inline void write_colour(const Vector3 &colour, Entity *entity)
{
2021-08-04 20:23:18 +00:00
char value[64];
2020-11-17 11:16:16 +00:00
2021-08-04 20:23:18 +00:00
sprintf(value, "%f %f %f", colour[0], colour[1], colour[2]);
entity->setKeyValue("_color", value);
2020-11-17 11:16:16 +00:00
}
class Colour {
2021-08-04 20:23:18 +00:00
Callback<void()> m_colourChanged;
Shader *m_state;
2020-11-17 11:16:16 +00:00
2021-08-04 20:23:18 +00:00
void capture_state()
{
m_state = colour_capture_state_fill(m_colour);
}
2020-11-17 11:16:16 +00:00
2021-08-04 20:23:18 +00:00
void release_state()
{
colour_release_state_fill(m_colour);
}
2020-11-17 11:16:16 +00:00
public:
2021-08-04 20:23:18 +00:00
Vector3 m_colour;
Colour(const Callback<void()> &colourChanged)
: m_colourChanged(colourChanged)
{
default_colour(m_colour);
capture_state();
}
~Colour()
{
release_state();
}
void colourChanged(const char *value)
{
release_state();
read_colour(m_colour, value);
capture_state();
m_colourChanged();
}
void colour255Changed(const char *value)
{
release_state();
read_colour255(m_colour, value);
capture_state();
m_colourChanged();
}
typedef MemberCaller<Colour, void (const char *), &Colour::colourChanged> ColourChangedCaller;
typedef MemberCaller<Colour, void (const char *), &Colour::colour255Changed> Colour255ChangedCaller;
void write(Entity *entity) const
{
write_colour(m_colour, entity);
}
Shader *state() const
{
return m_state;
}
2020-11-17 11:16:16 +00:00
};
#endif