mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-22 09:21:57 -05:00
Window contents move along with the window!
This commit is contained in:
parent
64127e0637
commit
6f6f9bd84d
10 changed files with 48 additions and 7 deletions
|
@ -2,6 +2,7 @@
|
||||||
#include "FrameBufferSDL.h"
|
#include "FrameBufferSDL.h"
|
||||||
#include "Widget.h"
|
#include "Widget.h"
|
||||||
#include "Font.h"
|
#include "Font.h"
|
||||||
|
#include "Window.h"
|
||||||
#include <AK/Assertions.h>
|
#include <AK/Assertions.h>
|
||||||
#include <SDL.h>
|
#include <SDL.h>
|
||||||
|
|
||||||
|
@ -9,6 +10,14 @@ Painter::Painter(Widget& widget)
|
||||||
: m_widget(widget)
|
: m_widget(widget)
|
||||||
, m_font(Font::defaultFont())
|
, 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()
|
Painter::~Painter()
|
||||||
|
@ -26,7 +35,7 @@ static dword* scanline(int y)
|
||||||
void Painter::fillRect(const Rect& rect, Color color)
|
void Painter::fillRect(const Rect& rect, Color color)
|
||||||
{
|
{
|
||||||
Rect r = rect;
|
Rect r = rect;
|
||||||
r.moveBy(m_widget.x(), m_widget.y());
|
r.moveBy(m_translation);
|
||||||
|
|
||||||
for (int y = r.top(); y < r.bottom(); ++y) {
|
for (int y = r.top(); y < r.bottom(); ++y) {
|
||||||
dword* bits = scanline(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)
|
void Painter::drawRect(const Rect& rect, Color color)
|
||||||
{
|
{
|
||||||
Rect r = rect;
|
Rect r = rect;
|
||||||
r.moveBy(m_widget.x(), m_widget.y());
|
r.moveBy(m_translation);
|
||||||
|
|
||||||
for (int y = r.top(); y < r.bottom(); ++y) {
|
for (int y = r.top(); y < r.bottom(); ++y) {
|
||||||
dword* bits = scanline(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)
|
void Painter::xorRect(const Rect& rect, Color color)
|
||||||
{
|
{
|
||||||
Rect r = rect;
|
Rect r = rect;
|
||||||
r.moveBy(m_widget.x(), m_widget.y());
|
r.moveBy(m_translation);
|
||||||
|
|
||||||
for (int y = r.top(); y < r.bottom(); ++y) {
|
for (int y = r.top(); y < r.bottom(); ++y) {
|
||||||
dword* bits = scanline(y);
|
dword* bits = scanline(y);
|
||||||
|
@ -79,12 +88,12 @@ void Painter::drawText(const Rect& rect, const String& text, TextAlignment align
|
||||||
|
|
||||||
if (alignment == TextAlignment::TopLeft) {
|
if (alignment == TextAlignment::TopLeft) {
|
||||||
point = rect.location();
|
point = rect.location();
|
||||||
point.moveBy(m_widget.x(), m_widget.y());
|
point.moveBy(m_translation);
|
||||||
} else if (alignment == TextAlignment::Center) {
|
} else if (alignment == TextAlignment::Center) {
|
||||||
int textWidth = text.length() * m_font.glyphWidth();
|
int textWidth = text.length() * m_font.glyphWidth();
|
||||||
point = rect.center();
|
point = rect.center();
|
||||||
point.moveBy(-(textWidth / 2), -(m_font.glyphWidth() / 2));
|
point.moveBy(-(textWidth / 2), -(m_font.glyphWidth() / 2));
|
||||||
point.moveBy(m_widget.x(), m_widget.y());
|
point.moveBy(m_translation);
|
||||||
} else {
|
} else {
|
||||||
ASSERT_NOT_REACHED();
|
ASSERT_NOT_REACHED();
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,4 +24,5 @@ public:
|
||||||
private:
|
private:
|
||||||
Widget& m_widget;
|
Widget& m_widget;
|
||||||
Font& m_font;
|
Font& m_font;
|
||||||
|
Point m_translation;
|
||||||
};
|
};
|
||||||
|
|
|
@ -17,6 +17,11 @@ public:
|
||||||
m_y += dy;
|
m_y += dy;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void moveBy(const Point& delta)
|
||||||
|
{
|
||||||
|
moveBy(delta.x(), delta.y());
|
||||||
|
}
|
||||||
|
|
||||||
bool operator==(const Point& other) const
|
bool operator==(const Point& other) const
|
||||||
{
|
{
|
||||||
return m_x == other.m_x
|
return m_x == other.m_x
|
||||||
|
|
|
@ -22,6 +22,11 @@ public:
|
||||||
m_location.moveBy(dx, dy);
|
m_location.moveBy(dx, dy);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void moveBy(const Point& delta)
|
||||||
|
{
|
||||||
|
m_location.moveBy(delta);
|
||||||
|
}
|
||||||
|
|
||||||
Point center() const
|
Point center() const
|
||||||
{
|
{
|
||||||
return { x() + width() / 2, y() + height() / 2 };
|
return { x() + width() / 2, y() + height() / 2 };
|
||||||
|
|
|
@ -16,7 +16,7 @@ TerminalWidget::TerminalWidget(Widget* parent)
|
||||||
|
|
||||||
auto& font = Font::defaultFont();
|
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());
|
printf("rekt: %d x %d\n", width(), height());
|
||||||
m_screen = new CharacterWithAttributes[rows() * columns()];
|
m_screen = new CharacterWithAttributes[rows() * columns()];
|
||||||
|
|
|
@ -105,3 +105,9 @@ Widget::HitTestResult Widget::hitTest(int x, int y)
|
||||||
return { this, x, y };
|
return { this, x, y };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Widget::setWindow(Window* window)
|
||||||
|
{
|
||||||
|
if (m_window == window)
|
||||||
|
return;
|
||||||
|
m_window = window;
|
||||||
|
}
|
||||||
|
|
|
@ -62,6 +62,8 @@ public:
|
||||||
return m_window;
|
return m_window;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setWindow(Window*);
|
||||||
|
|
||||||
Widget* parentWidget() { return static_cast<Widget*>(parent()); }
|
Widget* parentWidget() { return static_cast<Widget*>(parent()); }
|
||||||
const Widget* parentWidget() const { return static_cast<const Widget*>(parent()); }
|
const Widget* parentWidget() const { return static_cast<const Widget*>(parent()); }
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#include "Window.h"
|
#include "Window.h"
|
||||||
#include "WindowManager.h"
|
#include "WindowManager.h"
|
||||||
#include "Event.h"
|
#include "Event.h"
|
||||||
|
#include "Widget.h"
|
||||||
|
|
||||||
Window::Window(Object* parent)
|
Window::Window(Object* parent)
|
||||||
: Object(parent)
|
: Object(parent)
|
||||||
|
@ -18,6 +19,7 @@ void Window::setMainWidget(Widget* widget)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
m_mainWidget = widget;
|
m_mainWidget = widget;
|
||||||
|
widget->setWindow(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Window::setTitle(String&& title)
|
void Window::setTitle(String&& title)
|
||||||
|
@ -47,5 +49,12 @@ void Window::event(Event& event)
|
||||||
return Object::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);
|
return Object::event(event);
|
||||||
}
|
}
|
||||||
|
|
|
@ -185,6 +185,10 @@ void WindowManager::handlePaintEvent(PaintEvent& event)
|
||||||
//printf("[WM] paint event\n");
|
//printf("[WM] paint event\n");
|
||||||
m_rootWidget->event(event);
|
m_rootWidget->event(event);
|
||||||
paintWindowFrames();
|
paintWindowFrames();
|
||||||
|
|
||||||
|
for (auto* window : m_windows) {
|
||||||
|
window->event(event);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void WindowManager::event(Event& event)
|
void WindowManager::event(Event& event)
|
||||||
|
|
|
@ -42,7 +42,7 @@ int main(int c, char** v)
|
||||||
win->setTitle("Console");
|
win->setTitle("Console");
|
||||||
win->setRect({100, 300, 644, 254});
|
win->setRect({100, 300, 644, 254});
|
||||||
|
|
||||||
auto* t = new TerminalWidget(&w);
|
auto* t = new TerminalWidget(nullptr);
|
||||||
win->setMainWidget(t);
|
win->setMainWidget(t);
|
||||||
|
|
||||||
return loop.exec();
|
return loop.exec();
|
||||||
|
|
Loading…
Reference in a new issue