Skip to content

Commit

Permalink
Patch(Scaffolding): Different quotes for different animation sources (#…
Browse files Browse the repository at this point in the history
…10)

Co-authored-by: Moritz Jung <m.projects.code@gmail.com>
  • Loading branch information
sigrunixia and mProjectsCode authored Aug 13, 2023
1 parent c0d92a3 commit 5f95980
Show file tree
Hide file tree
Showing 10 changed files with 291 additions and 147 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ Inspired by the saintly [Gemmy](https://github.com/ericaxu/gemmy), the Obsidian
## Features

### Multiple Characters
- Gemmy, the original [Obsidian](https://www.obsidian.md/) Shard who has all new quotes and is now 100% even more unhelpful.
- Granite, Gemmy's rival in all things. She'll not stop until that purple shard succumbs to the power of her words. (:sparkles: Coming Soon)
- Drake, the dashing and devious dragon who loves nothing more than to talk about dice. (:sparkles: Coming Soon)

- Gemmy, the original [Obsidian](https://www.obsidian.md/) Shard who has all new quotes and is now 100% even more unhelpful.
- Granite, Gemmy's rival in all things. She'll not stop until that purple shard succumbs to the power of her words. (:sparkles: Coming Soon)
- Drake, the dashing and devious dragon who loves nothing more than to talk about dice. (:sparkles: Coming Soon)

### Random Quotes

Expand Down
192 changes: 122 additions & 70 deletions main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "granite",
"name": "Granite",
"version": "0.1.0",
"version": "0.1.1",
"minAppVersion": "0.19.0",
"description": "A Goal Reinforcing And Note Improving Tiny Entity",
"author": "Obsidian, and the Obsidian TTRPG Community",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@obsidian-ttrpg-community/granite",
"version": "0.1.0",
"version": "0.1.1",
"description": "A Goal Reinforcing And Note Improving Tiny Entity",
"main": "main.js",
"scripts": {
Expand Down
2 changes: 2 additions & 0 deletions src/Animations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ export class Animations {
[AnimationSourceType.DRAKE]: this.drakeAnimationMap,
[AnimationSourceType.GEMMY]: this.gemmyAnimationMap,
};

console.log('granite | build animation map', this.animationSourceMap);
}

/**
Expand Down
42 changes: 25 additions & 17 deletions src/Granite.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { Animations, AnimationType } from './Animations';
import GranitePlugin from './main';
import { GRANITE_IDLE_QUOTES, WRITING_MODE_QUOTES } from './quotes/Quotes';
import { QuoteManager } from './QuoteManager';

const BUBBLE_DURATION: number = 5000;

export enum GraniteMode {
IDLE = 'IDLE',
WRITING = 'WRITING',
}

export enum GraniteState {
VISIBLE = 'VISIBLE',
INVISIBLE = 'INVISIBLE',
Expand All @@ -14,12 +19,13 @@ export enum GraniteState {
export class Granite {
public readonly plugin: GranitePlugin;
private readonly animations: Animations;
private readonly quotes: QuoteManager;

graniteEl: HTMLElement;
imageEl: HTMLElement;

state: GraniteState;
inWritingMode: boolean = false;
mode: GraniteMode;

idleTimeout: number;
writingModeTimeout: number;
Expand All @@ -28,8 +34,10 @@ export class Granite {
this.plugin = plugin;

this.animations = new Animations(this.plugin);
this.quotes = new QuoteManager(this.plugin);

this.state = GraniteState.INVISIBLE;
this.mode = GraniteMode.IDLE;

this.createGraniteEl();

Expand All @@ -46,17 +54,17 @@ export class Granite {

this.graniteEl.addEventListener('mouseenter', () => {
// ignore mouse events in writing mode
if (this.inWritingMode) {
if (this.mode === GraniteMode.WRITING) {
return;
}

this.saySomething(GRANITE_IDLE_QUOTES, true);
this.saySomething(true);
this.idleTimeout && clearTimeout(this.idleTimeout);
});

this.graniteEl.addEventListener('mouseleave', () => {
// ignore mouse events in writing mode
if (this.inWritingMode) {
if (this.mode === GraniteMode.WRITING) {
return;
}

Expand All @@ -78,7 +86,7 @@ export class Granite {
this.graniteEl.hidden = false;

// Quicker if we're in writing mode
if (this.inWritingMode) {
if (this.mode === GraniteMode.WRITING) {
this.animations.play(this.imageEl, AnimationType.POP_MOTION);

await sleep(1800);
Expand All @@ -89,7 +97,7 @@ export class Granite {
}

this.state = GraniteState.VISIBLE;
this.saySomething(WRITING_MODE_QUOTES, true);
this.saySomething(true);
} else {
this.animations.play(this.imageEl, AnimationType.EMERGE);

Expand Down Expand Up @@ -140,7 +148,7 @@ export class Granite {
}

onEditorChange(): void {
if (!this.inWritingMode) {
if (this.mode === GraniteMode.IDLE) {
return;
}

Expand All @@ -151,14 +159,14 @@ export class Granite {
}

enterWritingMode(): void {
this.inWritingMode = true;
this.mode = GraniteMode.WRITING;
this.disappear();

this.setWritingModeTimeout();
}

leaveWritingMode() {
this.inWritingMode = false;
this.mode = GraniteMode.IDLE;
this.reset();

window.clearTimeout(this.writingModeTimeout);
Expand All @@ -170,7 +178,7 @@ export class Granite {
}

this.writingModeTimeout = window.setTimeout(() => {
if (this.inWritingMode) {
if (this.mode === GraniteMode.WRITING) {
this.appear();
}
}, this.plugin.settings.writingModeGracePeriod * 1000);
Expand All @@ -187,27 +195,27 @@ export class Granite {
}

this.idleTimeout = window.setTimeout(() => {
if (this.inWritingMode) {
if (this.mode === GraniteMode.WRITING) {
return;
}

this.saySomething(GRANITE_IDLE_QUOTES, false);
this.saySomething(false);
this.startNextIdleTimeout();
}, randomizedTimeout);
}

async saySomething(quotes: string[], persistent: boolean): Promise<void> {
async saySomething(persistent: boolean): Promise<void> {
if (this.state !== GraniteState.VISIBLE) {
return;
}

const randomThing = quotes[Math.floor(Math.random() * quotes.length)];
const quote: string = this.quotes.getQuote();

this.graniteEl.setAttr('aria-label', randomThing);
this.graniteEl.setAttr('aria-label', quote);
this.graniteEl.setAttr('aria-label-position', 'top');
this.graniteEl.dispatchEvent(new MouseEvent('mouseover', { bubbles: true, clientX: 10, clientY: 10 }));

if (this.inWritingMode) {
if (this.mode === GraniteMode.WRITING) {
this.animations.play(this.imageEl, AnimationType.ANGRY_MOTION);

await sleep(1000);
Expand Down
41 changes: 41 additions & 0 deletions src/QuoteManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import GranitePlugin from './main';
import { AnimationSourceType } from './Setting';
import { GraniteMode, GraniteState } from './Granite';
import { DRAKE_EXCLUSIVE_QUOTES, GEMMY_EXCLUSIVE_QUOTES, GRANITE_BASE_QUOTES, Quotes } from './quotes/Quotes';

export class QuoteManager {
public readonly plugin: GranitePlugin;

public readonly quoteMap: Record<AnimationSourceType, Record<GraniteMode, string[]>>;

constructor(plugin: GranitePlugin) {
this.plugin = plugin;

this.quoteMap = {
[AnimationSourceType.GEMMY]: {
[GraniteMode.IDLE]: this.getIdleQuotes(GEMMY_EXCLUSIVE_QUOTES),
[GraniteMode.WRITING]: this.getWritingQuotes(GEMMY_EXCLUSIVE_QUOTES),
},
[AnimationSourceType.DRAKE]: {
[GraniteMode.IDLE]: this.getIdleQuotes(DRAKE_EXCLUSIVE_QUOTES),
[GraniteMode.WRITING]: this.getWritingQuotes(DRAKE_EXCLUSIVE_QUOTES),
},
};

console.log('granite | build quote map', this.quoteMap);
}

private getIdleQuotes(exclusiveQuotes: Quotes): string[] {
return [...GRANITE_BASE_QUOTES.general, ...GRANITE_BASE_QUOTES.idle, ...exclusiveQuotes.general, ...exclusiveQuotes.idle];
}

private getWritingQuotes(exclusiveQuotes: Quotes): string[] {
return [...GRANITE_BASE_QUOTES.general, ...GRANITE_BASE_QUOTES.writingMode, ...exclusiveQuotes.general, ...exclusiveQuotes.writingMode];
}

public getQuote(): string {
const quotes: string[] = this.quoteMap[this.plugin.settings.animationSource][this.plugin.granite.mode];

return quotes[Math.floor(Math.random() * quotes.length)];
}
}
18 changes: 17 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { debounce, Plugin } from 'obsidian';
import { DEFAULT_SETTINGS, GraniteSettings, GraniteSettingsTab } from './Setting';
import { AnimationSourceType, DEFAULT_SETTINGS, GraniteSettings, GraniteSettingsTab } from './Setting';
import { Granite } from './Granite';

export default class GranitePlugin extends Plugin {
settings: GraniteSettings;
granite: Granite;

async onload() {
console.log('granite | load');

await this.loadSettings();

this.granite = new Granite(this);
Expand Down Expand Up @@ -60,11 +62,25 @@ export default class GranitePlugin extends Plugin {
}

onunload() {
console.log('granite | unload');

this.granite.disappear();
}

async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());

// migrations
// @ts-ignore
if (this.settings.animationSource === 'dragon') {
this.settings.animationSource = AnimationSourceType.DRAKE;
}
// @ts-ignore
if (this.settings.animationSource === 'original') {
this.settings.animationSource = AnimationSourceType.GEMMY;
}

await this.saveSettings();
}

async saveSettings() {
Expand Down
Loading

0 comments on commit 5f95980

Please sign in to comment.