Level editor work

This commit is contained in:
elasota
2020-01-04 01:19:01 -05:00
parent ec7e511cdd
commit a4b8db1065
29 changed files with 358 additions and 168 deletions

View File

@@ -11,6 +11,7 @@
#include "PLInvisibleWidget.h"
#include "PLLabelWidget.h"
#include "PLPasStr.h"
#include "PLStandardColors.h"
#include "PLSysCalls.h"
#include "PLTimeTaggedVOSEvent.h"
#include "PLWidgets.h"
@@ -77,12 +78,13 @@ namespace PortabilityLayer
Window *GetWindow() const override;
ArrayView<const DialogItem> GetItems() const override;
void SetItemVisibility(unsigned int itemIndex, bool isVisible) override;
int16_t ExecuteModal(DialogFilterFunc_t filterFunc) override;
bool Populate(DialogTemplate *tmpl);
void DrawControls();
void DrawControls(bool redraw);
Point MouseToDialog(const GpMouseInputEvent &evt);
@@ -192,6 +194,26 @@ namespace PortabilityLayer
return ArrayView<const DialogItem>(m_items, m_numItems);
}
void DialogImpl::SetItemVisibility(unsigned int itemIndex, bool isVisible)
{
Widget *widget = m_items[itemIndex].GetWidget();
if (widget->IsVisible() != isVisible)
{
widget->SetVisible(isVisible);
DrawSurface *surface = m_window->GetDrawSurface();
if (isVisible)
widget->DrawControl(surface);
else
{
surface->SetForeColor(StdColors::Red());
surface->FrameRect(surface->m_port.GetRect());
}
}
}
int16_t DialogImpl::ExecuteModal(DialogFilterFunc_t filterFunc)
{
Window *window = this->GetWindow();
@@ -297,14 +319,22 @@ namespace PortabilityLayer
return true;
}
void DialogImpl::DrawControls()
void DialogImpl::DrawControls(bool redraw)
{
DrawSurface *surface = m_window->GetDrawSurface();
if (redraw)
{
surface->SetForeColor(StdColors::White());
surface->FillRect(surface->m_port.GetRect());
}
for (ArrayViewIterator<const DialogItem> it = GetItems().begin(), itEnd = GetItems().end(); it != itEnd; ++it)
{
const DialogItem &item = *it;
item.GetWidget()->DrawControl(surface);
Widget *widget = item.GetWidget();
if (widget->IsVisible())
widget->DrawControl(surface);
}
}
@@ -469,7 +499,7 @@ namespace PortabilityLayer
return nullptr;
}
dialog->DrawControls();
dialog->DrawControls(true);
return dialog;
}

View File

@@ -21,6 +21,7 @@ namespace PortabilityLayer
virtual HostMutex *CreateMutex() = 0;
virtual HostThreadEvent *CreateThreadEvent(bool autoReset, bool startSignaled) = 0;
virtual size_t GetFreeMemoryCosmetic() const = 0; // Returns free memory in bytes, does not have to be accurate
virtual void Beep() const = 0;
static void SetInstance(HostSystemServices *instance);
static HostSystemServices *GetInstance();

View File

@@ -18,7 +18,7 @@
// ICON format is just a 32x32 bitfield
struct IconImagePrefix
{
{
BEUInt32_t m_unknown; // Seems to always be zero
BEUInt16_t m_pitch; // +0x8000 for color
};
@@ -36,12 +36,13 @@ struct ColorIconSerializedData
};
namespace PortabilityLayer
{
{
class IconLoaderImpl final : public IconLoader
{
public:
bool LoadColorIcon(const int16_t id, THandle<PixMapImpl> &outColorImage, THandle<PixMapImpl> &outBWImage, THandle<PixMapImpl> &outMaskImage) override;
THandle<PixMapImpl> LoadBWIcon(const int16_t id) override;
THandle<PixMapImpl> LoadSimpleColorIcon(const THandle<void> &hdl) override;
THandle<PixMapImpl> LoadBWIcon(const THandle<void> &hdl) override;
static IconLoaderImpl *GetInstance();
@@ -238,10 +239,61 @@ namespace PortabilityLayer
return true;
}
THandle<PixMapImpl> IconLoaderImpl::LoadBWIcon(const int16_t id)
THandle<PixMapImpl> IconLoaderImpl::LoadSimpleColorIcon(const THandle<void> &hdl)
{
PL_NotYetImplemented();
return THandle<PixMapImpl>();
if (hdl == nullptr || hdl.MMBlock()->m_size != 1024)
return THandle<PixMapImpl>();
const Rect rect = Rect::Create(0, 0, 32, 32);
THandle<PixMapImpl> pixMap = PixMapImpl::Create(rect, GpPixelFormats::k8BitStandard);
if (!pixMap)
return THandle<PixMapImpl>();
const uint8_t *inData = static_cast<const uint8_t*>(*hdl);
uint8_t *outData = static_cast<uint8_t*>((*pixMap)->GetPixelData());
const size_t outPitch = (*pixMap)->GetPitch();
for (size_t row = 0; row < 32; row++)
{
for (size_t col = 0; col < 32; col++)
outData[col] = inData[col];
inData += 32;
outData += outPitch;
}
return pixMap;
}
THandle<PixMapImpl> IconLoaderImpl::LoadBWIcon(const THandle<void> &hdl)
{
if (hdl == nullptr || hdl.MMBlock()->m_size != 128)
return THandle<PixMapImpl>();
const Rect rect = Rect::Create(0, 0, 32, 32);
THandle<PixMapImpl> pixMap = PixMapImpl::Create(rect, GpPixelFormats::kBW1);
if (!pixMap)
return THandle<PixMapImpl>();
const uint8_t *inData = static_cast<const uint8_t*>(*hdl);
uint8_t *outData = static_cast<uint8_t*>((*pixMap)->GetPixelData());
const size_t outPitch = (*pixMap)->GetPitch();
for (size_t row = 0; row < 32; row++)
{
for (size_t col = 0; col < 32; col++)
{
if (inData[col / 8] & (0x80 >> (col & 7)))
outData[col] = 0xff;
else
outData[col] = 0x00;
}
inData += 4;
outData += outPitch;
}
return pixMap;
}
IconLoaderImpl *IconLoaderImpl::GetInstance()

View File

@@ -14,7 +14,8 @@ namespace PortabilityLayer
{
public:
virtual bool LoadColorIcon(const int16_t id, THandle<PixMapImpl> &outColorImage, THandle<PixMapImpl> &outBWImage, THandle<PixMapImpl> &outMaskImage) = 0;
virtual THandle<PixMapImpl> LoadBWIcon(const int16_t id) = 0;
virtual THandle<PixMapImpl> LoadSimpleColorIcon(const THandle<void> &hdl) = 0;
virtual THandle<PixMapImpl> LoadBWIcon(const THandle<void> &hdl) = 0;
static IconLoader *GetInstance();
};

View File

@@ -442,8 +442,6 @@ namespace PortabilityLayer
if (m_lastMenu == menu)
m_lastMenu = menuPtr->prevMenu;
menu.Dispose();
DrawMenuBar();
}

View File

@@ -1,5 +1,6 @@
#include "PLApplication.h"
#include "PLCore.h"
#include "PLCore.h"
#include "HostSystemServices.h"
#include <string.h>
#include <assert.h>
@@ -18,6 +19,6 @@ namespace PortabilityLayer
}
void SysBeep(int duration)
{
PL_NotYetImplemented();
{
PortabilityLayer::HostSystemServices::GetInstance()->Beep();
}

View File

@@ -1,12 +1,15 @@
#include "PLButtonWidget.h"
#include "PLCore.h"
#include "PLTimeTaggedVOSEvent.h"
#include "PLStandardColors.h"
#include "FontFamily.h"
namespace PortabilityLayer
{
ButtonWidget::ButtonWidget(const WidgetBasicState &state)
: WidgetSpec<ButtonWidget>(state)
, m_haveMouseDown(false)
, m_text(state.m_text)
{
}
@@ -51,4 +54,14 @@ namespace PortabilityLayer
(void)state;
return true;
}
void ButtonWidget::DrawControl(DrawSurface *surface)
{
surface->SetForeColor(StdColors::Black());
surface->FrameRect(this->m_rect);
surface->SetSystemFont(12, PortabilityLayer::FontFamilyFlag_Bold);
int32_t x = (m_rect.left + m_rect.right - static_cast<int32_t>(surface->MeasureString(m_text.ToShortStr()))) / 2;
int32_t y = (m_rect.top + m_rect.bottom + static_cast<int32_t>(surface->MeasureFontAscender())) / 2;
surface->DrawString(Point::Create(x, y), m_text.ToShortStr());
}
}

View File

@@ -12,6 +12,8 @@ namespace PortabilityLayer
bool Init(const WidgetBasicState &state) override;
void DrawControl(DrawSurface *surface) override;
WidgetHandleState_t ProcessEvent(const TimeTaggedVOSEvent &evt) override;
private:

View File

@@ -52,7 +52,6 @@ void ModalDialog(ModalFilterUPP filter, short *item)
PL_NotYetImplemented();
}
void DisposeModalFilterUPP(ModalFilterUPP upp)
{
PL_NotYetImplemented();

View File

@@ -23,6 +23,8 @@ struct Dialog
virtual Window *GetWindow() const = 0;
virtual ArrayView<const PortabilityLayer::DialogItem> GetItems() const = 0;
virtual void SetItemVisibility(unsigned int itemIndex, bool isVisible) = 0;
virtual int16_t ExecuteModal(DialogFilterFunc_t filterFunc) = 0;
};

View File

@@ -1101,7 +1101,7 @@ void DrawSurface::InvertFrameRect(const Rect &rect, const uint8_t *pattern)
void DrawSurface::InvertFillRect(const Rect &rect, const uint8_t *pattern)
{
PL_NotYetImplemented();
PL_NotYetImplemented_TODO("InvertFillRect");
}
void DrawSurface::SetForeColor(const PortabilityLayer::RGBAColor &color)

View File

@@ -30,6 +30,7 @@ namespace PortabilityLayer
void SetResLoad(bool load) override;
ResourceFile *LoadResFile(VirtualDirectory_t virtualDir, const PLPasStr &filename) const override;
short OpenResFork(VirtualDirectory_t virtualDir, const PLPasStr &filename) override;
void CloseResFile(short ref) override;
PLError_t CreateBlankResFile(VirtualDirectory_t virtualDir, const PLPasStr &filename) override;
@@ -131,7 +132,7 @@ namespace PortabilityLayer
}
}
delete rf;
rf->Destroy();
}
ResourceManagerImpl *ResourceManagerImpl::GetInstance()
@@ -144,6 +145,28 @@ namespace PortabilityLayer
m_load = load;
}
ResourceFile *ResourceManagerImpl::LoadResFile(VirtualDirectory_t virtualDir, const PLPasStr &filename) const
{
IOStream *fStream = nullptr;
if (FileManager::GetInstance()->RawOpenFileResources(virtualDir, filename, EFilePermission_Read, true, GpFileCreationDispositions::kOpenExisting, fStream) != PLErrors::kNone)
return nullptr;
ResourceFile *resFile = ResourceFile::Create();
if (!resFile)
return nullptr;
bool loaded = resFile->Load(fStream);
fStream->Close();
if (!loaded)
{
resFile->Destroy();
return nullptr;
}
return resFile;
}
short ResourceManagerImpl::OpenResFork(VirtualDirectory_t virtualDir, const PLPasStr &filename)
{
const size_t numSlots = m_resFiles.size();
@@ -161,19 +184,11 @@ namespace PortabilityLayer
if (resFileIndex == 0x7fff)
return -1;
IOStream *fStream = nullptr;
if (FileManager::GetInstance()->RawOpenFileResources(virtualDir, filename, EFilePermission_Read, true, GpFileCreationDispositions::kOpenExisting, fStream) != PLErrors::kNone)
return -1;
ResourceFile *resFile = new ResourceFile();
bool loaded = resFile->Load(fStream);
fStream->Close();
ResourceFile *resFile = LoadResFile(virtualDir, filename);
if (!loaded)
{
delete resFile;
if (!resFile)
return -1;
}
ResFileSlot slot;
slot.m_resourceFile = resFile;
@@ -202,7 +217,10 @@ namespace PortabilityLayer
void ResourceManagerImpl::CloseResFile(short ref)
{
ResFileSlot &slot = m_resFiles[ref];
delete slot.m_resourceFile;
assert(slot.m_resourceFile != nullptr);
slot.m_resourceFile->Destroy();
slot.m_resourceFile = nullptr;
if (m_lastResFile == ref)
@@ -256,8 +274,9 @@ namespace PortabilityLayer
const ResFileSlot& slot = m_resFiles[searchIndex];
assert(slot.m_resourceFile);
if (MMHandleBlock *block = slot.m_resourceFile->GetResource(resType, id, m_load))
return THandle<void>(block);
THandle<void> resHdl = slot.m_resourceFile->GetResource(resType, id, m_load);
if (resHdl != nullptr)
return resHdl;
searchIndex = slot.m_prevFile;
}

View File

@@ -35,6 +35,16 @@ namespace PortabilityLayer
OnStateChanged();
}
void Widget::SetVisible(bool visible)
{
m_visible = visible;
}
bool Widget::IsVisible() const
{
return m_visible;
}
void Widget::SetHighlightStyle(int16_t style)
{
(void)style;
@@ -54,6 +64,7 @@ namespace PortabilityLayer
: m_rect(state.m_rect)
, m_window(state.m_window)
, m_enabled(state.m_enabled)
, m_visible(true)
, m_state(0)
{
}

View File

@@ -44,6 +44,10 @@ namespace PortabilityLayer
void SetEnabled(bool enabled);
void SetState(int16_t state);
void SetVisible(bool visible);
bool IsVisible() const;
virtual void SetString(const PLPasStr &str);
virtual void SetHighlightStyle(int16_t style);
@@ -63,6 +67,7 @@ namespace PortabilityLayer
Rect m_rect;
int16_t m_state;
bool m_enabled;
bool m_visible;
};
}

View File

@@ -356,18 +356,18 @@ namespace PortabilityLayer
return tl;
}
MMHandleBlock *ResourceFile::GetResource(const ResTypeID &resType, int id, bool load)
THandle<void> ResourceFile::GetResource(const ResTypeID &resType, int id, bool load)
{
const ResourceCompiledTypeList *tl = GetResourceTypeList(resType);
if (tl == nullptr)
return nullptr;
return THandle<void>();
ResourceCompiledRef *refStart = tl->m_firstRef;
ResourceCompiledRef *refEnd = refStart + tl->m_numRefs;
ResourceCompiledRef *ref = BinarySearch(refStart, refEnd, id, CompiledRefSearchPredicate);
if (ref == refEnd)
return nullptr;
return THandle<void>();
MMHandleBlock *handle = nullptr;
if (ref->m_handle != nullptr)
@@ -391,6 +391,21 @@ namespace PortabilityLayer
}
}
return handle;
return THandle<void>(handle);
}
ResourceFile *ResourceFile::Create()
{
void *storage = PortabilityLayer::MemoryManager::GetInstance()->Alloc(sizeof(ResourceFile));
if (!storage)
return nullptr;
return new (storage) ResourceFile();
}
void ResourceFile::Destroy()
{
this->~ResourceFile();
PortabilityLayer::MemoryManager::GetInstance()->Release(this);
}
}

View File

@@ -1,7 +1,6 @@
#pragma once
#ifndef __PL_RESOURCE_FILE_H__
#define __PL_RESOURCE_FILE_H__
#include "PLHandle.h"
#include "ResTypeID.h"
#include <stdint.h>
@@ -17,17 +16,19 @@ namespace PortabilityLayer
class ResourceFile
{
public:
ResourceFile();
~ResourceFile();
bool Load(IOStream *stream);
void GetAllResourceTypeLists(ResourceCompiledTypeList *&outTypeLists, size_t &outCount) const;
const ResourceCompiledTypeList *GetResourceTypeList(const ResTypeID &resType);
MMHandleBlock *GetResource(const ResTypeID &resType, int id, bool load);
THandle<void> GetResource(const ResTypeID &resType, int id, bool load);
static ResourceFile *Create();
void Destroy();
private:
ResourceFile();
~ResourceFile();
uint8_t *m_resDataBlob;
size_t m_resDataBlobSize;
@@ -48,5 +49,3 @@ namespace PortabilityLayer
static int CompiledTypeListSearchPredicate(const ResTypeID &resTypeID, const ResourceCompiledTypeList &typeList);
};
}
#endif

View File

@@ -10,6 +10,7 @@ namespace PortabilityLayer
{
struct MMHandleBlock;
struct ResourceCompiledRef;
class ResourceFile;
class ResTypeID;
class ResourceManager
@@ -20,6 +21,7 @@ namespace PortabilityLayer
virtual void SetResLoad(bool load) = 0;
virtual ResourceFile *LoadResFile(VirtualDirectory_t virtualDir, const PLPasStr &filename) const = 0;
virtual short OpenResFork(VirtualDirectory_t virtualDir, const PLPasStr &filename) = 0;
virtual void CloseResFile(short ref) = 0;
virtual PLError_t CreateBlankResFile(VirtualDirectory_t virtualDir, const PLPasStr &filename) = 0;

View File

@@ -13,6 +13,9 @@ struct Point
Point operator-(const Point &other) const;
Point operator+(const Point &other) const;
Point &operator-=(const Point &other);
Point &operator+=(const Point &other);
static Point Create(int16_t h, int16_t v);
};
@@ -117,6 +120,20 @@ inline Point Point::operator+(const Point &other) const
return Point::Create(this->h + other.h, this->v + other.v);
}
inline Point &Point::operator-=(const Point &other)
{
this->h -= other.h;
this->v -= other.v;
return *this;
}
inline Point &Point::operator+=(const Point &other)
{
this->h += other.h;
this->v += other.v;
return *this;
}
inline Point Point::Create(int16_t h, int16_t v)
{
Point p;