1
0
mirror of https://github.com/SerenityOS/serenity synced 2024-07-09 03:30:46 +00:00

Window contents move along with the window!

This commit is contained in:
Andreas Kling 2018-10-12 02:41:27 +02:00
parent 64127e0637
commit 6f6f9bd84d
10 changed files with 48 additions and 7 deletions

View File

@ -2,6 +2,7 @@
#include "FrameBufferSDL.h"
#include "Widget.h"
#include "Font.h"
#include "Window.h"
#include <AK/Assertions.h>
#include <SDL.h>
@ -9,6 +10,14 @@ Painter::Painter(Widget& widget)
: m_widget(widget)
, m_font(Font::defaultFont())
{
if (auto* window = widget.window()) {
printf("window :: %s\n", window->title().characters());
m_translation.setX(window->x());
m_translation.setY(window->y());
} else {
m_translation.setX(widget.x());
m_translation.setY(widget.y());
}
}
Painter::~Painter()
@ -26,7 +35,7 @@ static dword* scanline(int y)
void Painter::fillRect(const Rect& rect, Color color)
{
Rect r = rect;
r.moveBy(m_widget.x(), m_widget.y());
r.moveBy(m_translation);
for (int y = r.top(); y < r.bottom(); ++y) {
dword* bits = scanline(y);
@ -39,7 +48,7 @@ void Painter::fillRect(const Rect& rect, Color color)
void Painter::drawRect(const Rect& rect, Color color)
{
Rect r = rect;
r.moveBy(m_widget.x(), m_widget.y());
r.moveBy(m_translation);
for (int y = r.top(); y < r.bottom(); ++y) {
dword* bits = scanline(y);
@ -57,7 +66,7 @@ void Painter::drawRect(const Rect& rect, Color color)
void Painter::xorRect(const Rect& rect, Color color)
{
Rect r = rect;
r.moveBy(m_widget.x(), m_widget.y());
r.moveBy(m_translation);
for (int y = r.top(); y < r.bottom(); ++y) {
dword* bits = scanline(y);
@ -79,12 +88,12 @@ void Painter::drawText(const Rect& rect, const String& text, TextAlignment align
if (alignment == TextAlignment::TopLeft) {
point = rect.location();
point.moveBy(m_widget.x(), m_widget.y());
point.moveBy(m_translation);
} else if (alignment == TextAlignment::Center) {
int textWidth = text.length() * m_font.glyphWidth();
point = rect.center();
point.moveBy(-(textWidth / 2), -(m_font.glyphWidth() / 2));
point.moveBy(m_widget.x(), m_widget.y());
point.moveBy(m_translation);
} else {
ASSERT_NOT_REACHED();
}

View File

@ -24,4 +24,5 @@ public:
private:
Widget& m_widget;
Font& m_font;
Point m_translation;
};

View File

@ -17,6 +17,11 @@ public:
m_y += dy;
}
void moveBy(const Point& delta)
{
moveBy(delta.x(), delta.y());
}
bool operator==(const Point& other) const
{
return m_x == other.m_x

View File

@ -22,6 +22,11 @@ public:
m_location.moveBy(dx, dy);
}
void moveBy(const Point& delta)
{
m_location.moveBy(delta);
}
Point center() const
{
return { x() + width() / 2, y() + height() / 2 };

View File

@ -16,7 +16,7 @@ TerminalWidget::TerminalWidget(Widget* parent)
auto& font = Font::defaultFont();
setRect({ 100, 300, (columns() * font.glyphWidth()) + 4, (rows() * font.glyphHeight()) + 4 });
setRect({ 0, 0, (columns() * font.glyphWidth()) + 4, (rows() * font.glyphHeight()) + 4 });
printf("rekt: %d x %d\n", width(), height());
m_screen = new CharacterWithAttributes[rows() * columns()];

View File

@ -105,3 +105,9 @@ Widget::HitTestResult Widget::hitTest(int x, int y)
return { this, x, y };
}
void Widget::setWindow(Window* window)
{
if (m_window == window)
return;
m_window = window;
}

View File

@ -62,6 +62,8 @@ public:
return m_window;
}
void setWindow(Window*);
Widget* parentWidget() { return static_cast<Widget*>(parent()); }
const Widget* parentWidget() const { return static_cast<const Widget*>(parent()); }

View File

@ -1,6 +1,7 @@
#include "Window.h"
#include "WindowManager.h"
#include "Event.h"
#include "Widget.h"
Window::Window(Object* parent)
: Object(parent)
@ -18,6 +19,7 @@ void Window::setMainWidget(Widget* widget)
return;
m_mainWidget = widget;
widget->setWindow(this);
}
void Window::setTitle(String&& title)
@ -47,5 +49,12 @@ void Window::event(Event& event)
return Object::event(event);
}
if (event.isPaintEvent()) {
if (m_mainWidget) {
printf("forward to main widget\n");
return m_mainWidget->event(event);
}
}
return Object::event(event);
}

View File

@ -185,6 +185,10 @@ void WindowManager::handlePaintEvent(PaintEvent& event)
//printf("[WM] paint event\n");
m_rootWidget->event(event);
paintWindowFrames();
for (auto* window : m_windows) {
window->event(event);
}
}
void WindowManager::event(Event& event)

View File

@ -42,7 +42,7 @@ int main(int c, char** v)
win->setTitle("Console");
win->setRect({100, 300, 644, 254});
auto* t = new TerminalWidget(&w);
auto* t = new TerminalWidget(nullptr);
win->setMainWidget(t);
return loop.exec();