/*
* This source file is part of RmlUi, the HTML/CSS Interface Middleware
*
* For the latest information, see http://github.com/mikke89/RmlUi
*
* Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
* Copyright (c) 2019 The RmlUi Team, and contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#include "DecoratorGradient.h"
#include "../../Include/RmlUi/Core/Element.h"
#include "../../Include/RmlUi/Core/Geometry.h"
#include "../../Include/RmlUi/Core/GeometryUtilities.h"
#include "../../Include/RmlUi/Core/ElementUtilities.h"
/*
Gradient decorator usage in CSS:
decorator: gradient( direction start-color stop-color );
direction: horizontal|vertical;
start-color: #ff00ff;
stop-color: #00ff00;
*/
namespace Rml {
namespace Core {
//=======================================================
DecoratorGradient::DecoratorGradient()
{
}
DecoratorGradient::~DecoratorGradient()
{
}
bool DecoratorGradient::Initialise(const Direction &dir_, const Colourb &start_, const Colourb & stop_)
{
dir = dir_;
start = start_;
stop = stop_;
return true;
}
DecoratorDataHandle DecoratorGradient::GenerateElementData(Element* element) const
{
auto *data = new Geometry(element);
Vector2f padded_size = element->GetBox().GetSize(Box::PADDING);
const float opacity = element->GetComputedValues().opacity;
// Apply opacity
Colourb colour_start = start;
colour_start.alpha = (byte)(opacity * (float)colour_start.alpha);
Colourb colour_stop = stop;
colour_stop.alpha = (byte)(opacity * (float)colour_stop.alpha);
auto &vertices = data->GetVertices();
vertices.resize(4);
auto &indices = data->GetIndices();
indices.resize(6);
GeometryUtilities::GenerateQuad(&vertices[0], &indices[0], Vector2f(0, 0), padded_size, colour_start, 0);
if (dir == Direction::Horizontal) {
vertices[1].colour = vertices[2].colour = colour_stop;
} else if (dir == Direction::Vertical) {
vertices[2].colour = vertices[3].colour = colour_stop;
}
data->SetHostElement(element);
return reinterpret_cast(data);
}
void DecoratorGradient::ReleaseElementData(DecoratorDataHandle element_data) const
{
delete reinterpret_cast(element_data);
}
void DecoratorGradient::RenderElement(Element* element, DecoratorDataHandle element_data) const
{
auto* data = reinterpret_cast(element_data);
data->Render(element->GetAbsoluteOffset(Box::PADDING).Round());
}
//=======================================================
DecoratorGradientInstancer::DecoratorGradientInstancer()
{
// register properties for the decorator
ids.direction = RegisterProperty("direction", "horizontal").AddParser("keyword", "horizontal, vertical").GetId();
ids.start = RegisterProperty("start-color", "#ffffff").AddParser("color").GetId();
ids.stop = RegisterProperty("stop-color", "#ffffff").AddParser("color").GetId();
RegisterShorthand("decorator", "direction, start-color, stop-color", ShorthandType::FallThrough);
}
DecoratorGradientInstancer::~DecoratorGradientInstancer()
{
}
SharedPtr DecoratorGradientInstancer::InstanceDecorator(const String & RMLUI_UNUSED_PARAMETER(name), const PropertyDictionary& properties_,
const DecoratorInstancerInterface& RMLUI_UNUSED_PARAMETER(interface_))
{
RMLUI_UNUSED(name);
RMLUI_UNUSED(interface_);
DecoratorGradient::Direction dir = (DecoratorGradient::Direction)properties_.GetProperty(ids.direction)->Get< int >();
Colourb start = properties_.GetProperty(ids.start)->Get();
Colourb stop = properties_.GetProperty(ids.stop)->Get();
auto decorator = std::make_shared();
if (decorator->Initialise(dir, start, stop)) {
return decorator;
}
return nullptr;
}
}
}