Skip to content

Commit

Permalink
Add new Text tool (fix #28)
Browse files Browse the repository at this point in the history
This is the first (not yet production-ready) version of the
interactive Text tool. The text input is done with a transparent
ui::Entry, and on each text modification an ExtraCel is rendered with
this same ui::Entry's TextBlob to be displated in the canvas with the
active zoom level.

The ui::Entry is being painted along the text in the canvas (just for
testing), but this is something to be fixed. Probably it will not be
the case in the future and a fully customized rendering (onPaint())
process will be required.
  • Loading branch information
dacap committed Jun 13, 2024
1 parent 8d5e60f commit 6e06e9a
Show file tree
Hide file tree
Showing 35 changed files with 784 additions and 79 deletions.
Binary file modified data/extensions/aseprite-theme/dark/sheet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions data/extensions/aseprite-theme/dark/theme.xml
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@
<part id="tool_filled_ellipse" x="192" y="80" w="16" h="16" />
<part id="tool_contour" x="144" y="96" w="16" h="16" />
<part id="tool_polygon" x="160" y="96" w="16" h="16" />
<part id="tool_text" x="144" y="112" w="16" h="16" />
<part id="tool_blur" x="160" y="112" w="16" h="16" />
<part id="tool_jumble" x="176" y="112" w="16" h="16" />
<part id="tool_configuration" x="144" y="128" w="16" h="16" />
Expand Down
Binary file modified data/extensions/aseprite-theme/sheet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions data/extensions/aseprite-theme/theme.xml
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@
<part id="tool_filled_ellipse" x="192" y="80" w="16" h="16" />
<part id="tool_contour" x="144" y="96" w="16" h="16" />
<part id="tool_polygon" x="160" y="96" w="16" h="16" />
<part id="tool_text" x="144" y="112" w="16" h="16" />
<part id="tool_blur" x="160" y="112" w="16" h="16" />
<part id="tool_jumble" x="176" y="112" w="16" h="16" />
<part id="tool_configuration" x="144" y="128" w="16" h="16" />
Expand Down
13 changes: 12 additions & 1 deletion data/gui.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
<key command="Outline" shortcut="Shift+O" />
<key command="ColorCurve" shortcut="Ctrl+M" />
<key command="ColorCurve" shortcut="F10" />
<key command="PasteText" shortcut="T" />
<key command="Options" mac="Cmd+," />
<key command="Options" shortcut="Ctrl+K" mac="Cmd+K" />
<key command="KeyboardShortcuts" shortcut="Ctrl+Alt+Shift+K" mac="Cmd+Alt+Shift+K" />
Expand Down Expand Up @@ -611,6 +610,8 @@

<key tool="blur" shortcut="R" />
<key tool="jumble" shortcut="R" />

<key tool="text" shortcut="T" />
</tools>

<!-- Editor Quicktools: these are modifiers to select quickly a
Expand Down Expand Up @@ -1493,6 +1494,16 @@
default_brush_size="16" />
</group>

<group id="text">
<tool id="text"
text="@.text"
ink="text"
controller="two_points"
pointshape="pixel"
intertwine="as_rectangles"
traceepolicy="last" />
</group>

</tools>

</gui>
1 change: 1 addition & 0 deletions data/strings/en.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1808,6 +1808,7 @@ contour = Contour Tool
polygon = Polygon Tool
blur = Blur Tool
jumble = Jumble Tool
text = Text Tool
shortcut = Shortcut: {0}
preview_hide = Hide Preview
preview_show = Show Preview
Expand Down
2 changes: 2 additions & 0 deletions src/app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,11 @@ if(ENABLE_UI)
ui/editor/play_state.cpp
ui/editor/scrolling_state.cpp
ui/editor/select_box_state.cpp
ui/editor/select_text_box_state.cpp
ui/editor/standby_state.cpp
ui/editor/state_with_wheel_behavior.cpp
ui/editor/transform_handles.cpp
ui/editor/writing_text_state.cpp
ui/editor/zooming_state.cpp
ui/export_file_window.cpp
ui/expr_entry.cpp
Expand Down
27 changes: 3 additions & 24 deletions src/app/commands/cmd_paste_text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include "app/pref/preferences.h"
#include "app/ui/drop_down_button.h"
#include "app/ui/editor/editor.h"
#include "app/ui/skin/skin_theme.h"
#include "app/ui/timeline/timeline.h"
#include "app/util/render_text.h"
#include "base/fs.h"
Expand Down Expand Up @@ -75,21 +74,7 @@ void PasteTextCommand::onExecute(Context* ctx)
return;

Preferences& pref = Preferences::instance();
FontInfo fontInfo;

// Old configuration
if (!pref.textTool.fontFace().empty()) {
fontInfo = FontInfo(FontInfo::Type::File,
pref.textTool.fontFace(),
pref.textTool.fontSize(),
text::FontStyle(),
pref.textTool.antialias());
}
// New configuration
if (!pref.textTool.fontInfo().empty()) {
fontInfo = base::convert_to<FontInfo>(pref.textTool.fontInfo());
}

FontInfo fontInfo = FontInfo::getFromPreferences();
PasteTextWindow window(fontInfo, pref.colorBar.fgColor());

window.userText()->setText(last_text_used);
Expand All @@ -101,20 +86,14 @@ void PasteTextCommand::onExecute(Context* ctx)
last_text_used = window.userText()->text();

fontInfo = window.fontInfo();
pref.textTool.fontInfo(base::convert_to<std::string>(fontInfo));
if (!pref.textTool.fontFace().empty()) {
pref.textTool.fontFace.clearValue();
pref.textTool.fontSize.clearValue();
pref.textTool.antialias.clearValue();
}
fontInfo.updatePreferences();

try {
auto* theme = skin::SkinTheme::instance();
std::string text = window.userText()->text();
app::Color color = window.fontColor()->getColor();

doc::ImageRef image = render_text(
theme->fontMgr(), fontInfo, text,
fontInfo, text,
gfx::rgba(color.getRed(),
color.getGreen(),
color.getBlue(),
Expand Down
34 changes: 34 additions & 0 deletions src/app/font_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#endif

#include "app/font_info.h"
#include "app/pref/preferences.h"
#include "base/fs.h"
#include "base/split_string.h"
#include "fmt/format.h"
Expand Down Expand Up @@ -76,6 +77,39 @@ text::TypefaceRef FontInfo::findTypeface(const text::FontMgrRef& fontMgr) const
return nullptr;
}

// static
FontInfo FontInfo::getFromPreferences()
{
Preferences& pref = Preferences::instance();
FontInfo fontInfo;

// Old configuration
if (!pref.textTool.fontFace().empty()) {
fontInfo = FontInfo(FontInfo::Type::File,
pref.textTool.fontFace(),
pref.textTool.fontSize(),
text::FontStyle(),
pref.textTool.antialias());
}
// New configuration
if (!pref.textTool.fontInfo().empty()) {
fontInfo = base::convert_to<FontInfo>(pref.textTool.fontInfo());
}

return fontInfo;
}

void FontInfo::updatePreferences()
{
Preferences& pref = Preferences::instance();
pref.textTool.fontInfo(base::convert_to<std::string>(*this));
if (!pref.textTool.fontFace().empty()) {
pref.textTool.fontFace.clearValue();
pref.textTool.fontSize.clearValue();
pref.textTool.antialias.clearValue();
}
}

} // namespace app

namespace base {
Expand Down
3 changes: 3 additions & 0 deletions src/app/font_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ namespace app {

text::TypefaceRef findTypeface(const text::FontMgrRef& fontMgr) const;

static FontInfo getFromPreferences();
void updatePreferences();

bool operator==(const FontInfo& other) const {
return (m_type == other.m_type &&
m_name == other.m_name &&
Expand Down
5 changes: 4 additions & 1 deletion src/app/tools/ink.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2018-2020 Igara Studio S.A.
// Copyright (C) 2018-2022 Igara Studio S.A.
// Copyright (C) 2001-2017 David Capello
//
// This program is distributed under the terms of
Expand Down Expand Up @@ -68,6 +68,9 @@ namespace app {
// Returns true if this ink is used to mark slices
virtual bool isSlice() const { return false; }

// Returns true if this ink acts like the text tool
virtual bool isText() const { return false; }

// Returns true if this tool uses the dithering options
virtual bool withDitheringOptions() const { return false; }

Expand Down
12 changes: 12 additions & 0 deletions src/app/tools/inks.h
Original file line number Diff line number Diff line change
Expand Up @@ -529,5 +529,17 @@ class SelectionInk : public BaseInk {
};


class TextInk : public BaseInk {
public:
Ink* clone() override { return new TextInk(*this); }

bool isText() const override { return true; }

void prepareInk(ToolLoop* loop) override {
setProc(get_ink_proc<XorInkProcessing>(loop));
}
};


} // namespace tools
} // namespace app
2 changes: 2 additions & 0 deletions src/app/tools/tool_box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ const char* WellKnownInks::Slice = "slice";
const char* WellKnownInks::MoveSlice = "move_slice";
const char* WellKnownInks::Blur = "blur";
const char* WellKnownInks::Jumble = "jumble";
const char* WellKnownInks::Text = "text";

const char* WellKnownControllers::Freehand = "freehand";
const char* WellKnownControllers::PointByPoint = "point_by_point";
Expand Down Expand Up @@ -135,6 +136,7 @@ ToolBox::ToolBox()
m_inks[WellKnownInks::Slice] = new SliceInk();
m_inks[WellKnownInks::Blur] = new BlurInk();
m_inks[WellKnownInks::Jumble] = new JumbleInk();
m_inks[WellKnownInks::Text] = new TextInk();

m_controllers[WellKnownControllers::Freehand] = new FreehandController();
m_controllers[WellKnownControllers::PointByPoint] = new PointByPointController();
Expand Down
1 change: 1 addition & 0 deletions src/app/tools/tool_box.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ namespace app {
extern const char* MoveSlice;
extern const char* Blur;
extern const char* Jumble;
extern const char* Text;
};

namespace WellKnownControllers {
Expand Down
32 changes: 32 additions & 0 deletions src/app/ui/context_bar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "app/ui/dynamics_popup.h"
#include "app/ui/editor/editor.h"
#include "app/ui/expr_entry.h"
#include "app/ui/font_entry.h"
#include "app/ui/icon_button.h"
#include "app/ui/keyboard_shortcuts.h"
#include "app/ui/sampling_selector.h"
Expand Down Expand Up @@ -1854,6 +1855,24 @@ class ContextBar::SliceFields : public HBox {
std::string m_filter;
};

class ContextBar::FontSelector : public FontEntry {
public:
FontSelector(ContextBar* contextBar) {
// Load the font from the preferences
Preferences& pref = Preferences::instance();
setInfo(FontInfo::getFromPreferences());

FontChange.connect([contextBar](){
contextBar->FontChange();
});
}

~FontSelector() {
Preferences& pref = Preferences::instance();
info().updatePreferences();
}
};

ContextBar::ContextBar(TooltipManager* tooltipManager,
ColorBar* colorBar)
{
Expand Down Expand Up @@ -1908,6 +1927,7 @@ ContextBar::ContextBar(TooltipManager* tooltipManager,
m_symmetry->setVisible(pref.symmetryMode.enabled());

addChild(m_sliceFields = new SliceFields);
addChild(m_fontSelector = new FontSelector(this));

setupTooltips(tooltipManager);

Expand Down Expand Up @@ -2200,6 +2220,11 @@ void ContextBar::updateForTool(tools::Tool* tool)
(tool->getInk(0)->isSlice() ||
tool->getInk(1)->isSlice());

// True if the current tool is text tool.
const bool isText = tool &&
(tool->getInk(0)->isText() ||
tool->getInk(1)->isText());

// True if the current tool is floodfill
const bool isFloodfill = tool &&
(tool->getPointShape(0)->isFloodFill() ||
Expand Down Expand Up @@ -2284,6 +2309,8 @@ void ContextBar::updateForTool(tools::Tool* tool)
if (isSlice)
updateSliceFields(UIContext::instance()->activeSite());

m_fontSelector->setVisible(isText);

// Update ink shades with the current selected palette entries
if (updateShade)
m_inkShades->updateShadeFromColorBarPicks();
Expand Down Expand Up @@ -2571,6 +2598,11 @@ void ContextBar::setInkType(tools::InkType type)
m_inkType->setInkType(type);
}

FontInfo ContextBar::fontInfo() const
{
return m_fontSelector->info();
}

render::DitheringMatrix ContextBar::ditheringMatrix()
{
return m_ditheringSelector->ditheringMatrix();
Expand Down
9 changes: 8 additions & 1 deletion src/app/ui/context_bar.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2018-2022 Igara Studio S.A.
// Copyright (C) 2018-2024 Igara Studio S.A.
// Copyright (C) 2001-2017 David Capello
//
// This program is distributed under the terms of
Expand Down Expand Up @@ -52,6 +52,7 @@ namespace app {
class BrushSlot;
class ColorBar;
class DitheringSelector;
class FontInfo;
class GradientTypeSelector;
class SamplingSelector;
class Transformation;
Expand Down Expand Up @@ -89,6 +90,9 @@ namespace app {

void setInkType(tools::InkType type);

// For text tool
FontInfo fontInfo() const;

// For gradients
render::DitheringMatrix ditheringMatrix();
render::DitheringAlgorithmBase* ditheringAlgorithm();
Expand All @@ -99,6 +103,7 @@ namespace app {

// Signals
obs::signal<void()> BrushChange;
obs::signal<void()> FontChange;

protected:
void onInitTheme(ui::InitThemeEvent& ev) override;
Expand Down Expand Up @@ -163,6 +168,7 @@ namespace app {
class AutoSelectLayerField;
class SymmetryField;
class SliceFields;
class FontSelector;

ZoomButtons* m_zoomButtons;
SamplingSelector* m_samplingSelector;
Expand Down Expand Up @@ -201,6 +207,7 @@ namespace app {
ui::Label* m_selectBoxHelp;
SymmetryField* m_symmetry;
SliceFields* m_sliceFields;
FontSelector* m_fontSelector = nullptr;
obs::scoped_connection m_symmModeConn;
obs::scoped_connection m_fgColorConn;
obs::scoped_connection m_bgColorConn;
Expand Down
3 changes: 3 additions & 0 deletions src/app/ui/editor/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2230,6 +2230,9 @@ void Editor::onResize(ui::ResizeEvent& ev)
{
Widget::onResize(ev);
m_padding = calcExtraPadding(m_proj);

if (m_state)
m_state->onEditorResize(this);
}

void Editor::onPaint(ui::PaintEvent& ev)
Expand Down
5 changes: 5 additions & 0 deletions src/app/ui/editor/editor_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ namespace app {
// Called when the editor gets the focus.
virtual void onEditorGotFocus(Editor* editor) { }

// Called when the editor is resized. E.g. If a EditorState adds a
// temporary widget inside the editor, this method can layout that
// widget.
virtual void onEditorResize(Editor* editor) { }

// Called when the user presses a mouse button over the editor.
virtual bool onMouseDown(Editor* editor, ui::MouseMessage* msg) { return false; }

Expand Down
Loading

0 comments on commit 6e06e9a

Please sign in to comment.